<template>
  <div>
    <el-button size="small" type="primary" @click="onShow">新 增</el-button>
    <el-drawer
        title="新增返利代金券支出"
        :visible.sync="dialogFormVisible"
        size="40%"
        direction="rtl"
        :show-close="false"
        append-to-body
        @close="onClose(dialogFormVisible = false)">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position="right" size="mini">
            <el-col :span="12">
              <el-form-item label="企业名称:" prop="enterprisename">
                <el-popover
                    placement="bottom"
                    width="700"
                    trigger="click"
                    v-model="enterpriseShow"
                    @show="queryEnterprise">
                  <el-input
                      style="width:300px;margin-bottom: 10px"
                      :suffix-icon="enterpriseParam.content.where.condition?enterpriseParam.content.where.condition.length > 0?'':'':'el-icon-search'"
                      placeholder="请输入内容"
                      v-model="enterpriseParam.content.where.condition"
                      clearable
                      @clear="queryEnterprise(enterpriseParam.content.pageNumber = 1)"
                      size="mini"
                      @keyup.enter.native="queryEnterprise(enterpriseParam.content.pageNumber = 1)">
                  </el-input>
                  <el-table :data="enterprise.enterpriseData" @row-click="enterpriseData" height="396px" border size="mini">
                    <el-table-column
                        label="企业名称"
                        width="180">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.enterprisename?scope.row.enterprisename:'--' }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="省市县"
                        width="180">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.province?scope.row.province + '-' + scope.row.city + '-' + scope.row.county:'--' }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="详细地址"
                        width="0">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.address?scope.row.address:'--' }}</span>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div class="container normal-panel" style="text-align:right">
                    <el-pagination
                        background
                        small
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :page-sizes="[20,50,100,]"
                        layout="total,sizes, prev, pager, next, jumper"
                        :current-page="enterprise.currentPage"
                        :total="enterprise.total">
                    </el-pagination>
                  </div>
                  <el-input slot="reference" :readonly="true" v-model="form.enterprisename" autocomplete="off" placeholder="请选择企业" ></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="分类明细:" prop="subclass">
                <el-select v-model="form.subclass" placeholder="请选择分类明细" style="width: 100%">
                  <el-option
                      v-for="item in subClassData"
                      :key="item.value"
                      :label="item.value"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单据日期:" prop="billdate">
                <el-date-picker
                    style="width: 100%"
                    v-model="form.billdate"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="返利代金券:" prop="sa_cashbillid_rebate">
                <el-popover
                    placement="bottom"
                    width="700"
                    trigger="click"
                    v-model="cashCouponShow"
                    @show="queryCashCoupon">
                  <el-input
                      style="width:300px;margin-bottom: 10px"
                      placeholder="请输入内容"
                      v-model="cashCouponParam.content.where.condition"
                      clearable
                      @clear="queryCashCoupon(cashCouponParam.content.pageNumber = 1)"
                      size="mini"
                      @keyup.enter.native="queryCashCoupon(cashCouponParam.content.pageNumber = 1)">
                    <i slot="prefix" class="el-icon-search" @click="queryCashCoupon(cashCouponParam.content.pageNumber = 1)"></i>
                  </el-input>
                  <el-table :data="cashCoupon.cashCouponData" @row-click="cashCouponData" height="396px" border size="mini">
                    <el-table-column
                        label="单号"
                        width="180">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.billno?scope.row.billno:'--' }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="金额"
                        width="180">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.amount?scope.row.amount:'--' }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="分类明细"
                    >
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.subclass?scope.row.subclass:'--' }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="单据时间"
                    >
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.billdate?scope.row.billdate:'--' }}</span>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div class="container normal-panel" style="text-align:right">
                    <el-pagination
                        background
                        small
                        @size-change="handleSizeChangeCashCoupon"
                        @current-change="handleCurrentChangeCashCoupon"
                        :page-sizes="[10,20,50,100,]"
                        layout="total,sizes, prev, pager, next, jumper"
                        :current-page="cashCoupon.currentPage"
                        :total="cashCoupon.total">
                    </el-pagination>
                  </div>
                  <el-input slot="reference" :readonly="true"  v-model="form.sa_cashbillid_billno" autocomplete="off" placeholder="请选择返利代金券"  ></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支出金额:" prop="amount">
                <el-input v-model="form.amount" placeholder="请输入金额" type="number"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注:" prop="remarks">
                <el-input v-model="form.remarks" type="textarea" rows="5" placeholder="请输入备注信息"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="onClose" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "add",
  data() {
    var amountData = (rule,value,callback) =>{
      if (this.form.amount < 0 ){
        callback(new Error('不能下于0'))
      }else if(this.form.amount > this.cashCouponAmount){
        callback(new Error('不能超过代金券可用金额'))
      }
      else {
        callback();
      }
    }
    return {
      dialogFormVisible:false,
      enterpriseShow:false,
      cashCouponShow:false,
      cashCouponAmount:'',
      form:{
        "sa_cashbillid": 0,
        "sys_enterpriseid": '', //合作企业档案ID
        "remarks": "",
        "subclass": "", //明细分类
        "amount": '', //金额
        "billdate": "", //单据日期
        "enterprisename":"",
        "sa_cashbillid_rebate": '', //所选的代金券id
        "sa_cashbillid_billno":""
      },
      rules:{
        enterprisename:[
          { required: true, message: '请选择企业', trigger: 'change'},
        ],
        subclass:[
          { required: true, message: '请选择分类明细', trigger: 'change'},
        ],
        amount:[
          { required: true, validator: amountData, trigger: 'blur'},
        ],
        sa_cashbillid_rebate:[
          { required: true, message: '请选择返利代金券', trigger: 'change'},
        ],
      },
      subClassData:[],
      enterpriseParam:{
        content: {
          pageNumber: 1,
          pageSize: 20,
          where: {
            condition: "",
            type: 1,
            sa_projectid: ""
          }
        },
        id: 20220920083901
      },
      enterprise:{
        enterpriseData:[],
        total:0,
        currentPage:0,
      },
      cashCouponParam:{
        "id": 20230216192902,
        "content": {
          // "sys_enterpriseid": 2, //不传查全部，
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": "",
            "status": "审核"
          }
        }
      },
      cashCoupon:{
        cashCouponData:[],
        total:0,
        currentPage:0,
      }
    }
  },
  methods:{
    onSubmit(){
      console.log(this.form)
      this.$refs.form.validate(async(valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": 20230302140402,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.dialogFormVisible = false
          this.$refs.form.resetFields()
          this.form = {
            "sa_cashbillid": 0,
            "sys_enterpriseid": '', //合作企业档案ID
            "remarks": "",
            "subclass": "", //明细分类
            "amount": '', //金额
            "billdate": "", //单据日期
            "enterprisename":"",
            "sa_cashbillid_rebate": '', //所选的代金券id
            "sa_cashbillid_billno":""
          }
          this.$store.dispatch('changeDetailDrawer',true)
          this.$router.push({path:'/cashcouponPayDetail',query:{id:res.data.sa_cashbillid,rowindex:res.data.rowindex}})

        })
      })
    },
    onShow(){
      this.dialogFormVisible = true
      const newData = new Date()
      console.log(newData.getMonth())
      this.form.billdate = newData.getFullYear() + '-' + ((newData.getMonth() + 1) < 10? '0'+(newData.getMonth() + 1): (newData.getMonth() + 1)) + '-' + newData.getDate()
      this.subClassList()
    },
    /*获取分类明细*/
    subClassList(){
      this.$store.dispatch('optiontypeselect','rebatepaytype').then(res=>{
        console.log(res,'分类明细')
        /*let index = ''
        for (var i = 0 ;i<res.data.length; i++){
          if (res.data[i].value === '返利金'){
            index = i
          }
        }*/
        this.subClassData = res.data
      })
    },
    /*获取企业*/
    async queryEnterprise(){
      const res = await this.$api.requested(this.enterpriseParam)
      this.enterprise.enterpriseData = res.data
      this.enterprise.total = res.total
      this.enterprise.currentPage = res.pageNumber
    },
    enterpriseData(row){
      console.log(row,'row')
      this.form.sys_enterpriseid = row.sys_enterpriseid
      this.form.enterprisename = row.enterprisename
      this.enterpriseShow = false
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.enterpriseParam.content.pageSize = val
      this.queryEnterprise()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.enterpriseParam.content.pageNumber = val
      this.queryEnterprise()
    },
    /*获取返利代金券*/
    async queryCashCoupon(){
      this.cashCouponParam.content.where.condition = this.form.enterprisename
      const res = await this.$api.requested(this.cashCouponParam)
      this.cashCoupon.cashCouponData = res.data
      this.cashCoupon.total = res.total
      this.cashCoupon.currentPage = res.pageNumber
    },
    cashCouponData(row){
      console.log(row)
      this.form.sa_cashbillid_billno = row.billno
      this.form.sa_cashbillid_rebate = row.sa_cashbillid
      this.cashCouponAmount = row.amount
      this.cashCouponShow = false

    },
    handleSizeChangeCashCoupon(val) {
      // console.log(`每页 ${val} 条`);
      this.enterpriseParam.content.pageSize = val
      this.queryCashCoupon()
    },
    handleCurrentChangeCashCoupon(val) {
      // console.log(`当前页: ${val}`);
      this.enterpriseParam.content.pageNumber = val
      this.queryCashCoupon()
    },
    onClose(){
      this.$refs.form.resetFields()
      this.form = {
        "sa_cashbillid": 0,
        "sys_enterpriseid": '', //合作企业档案ID
        "remarks": "",
        "subclass": "", //明细分类
        "amount": '', //金额
        "billdate": "", //单据日期
        "enterprisename":"",
        "sa_cashbillid_rebate": '', //所选的代金券id
        "sa_cashbillid_billno":""
      }
    }
  }
}
</script>

<style scoped>

</style>