<template>
  <div>
    <el-button size="mini" type="primary" @click="onShow" :disabled="data.status !== '新建'">编 辑</el-button>
    <el-drawer
        title="编辑返利代金券"
        :visible.sync="dialogFormVisible"
        size="40%"
        direction="rtl"
        :show-close="false"
        append-to-body
        @close="dialogFormVisible = false">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="90px" 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="[10,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"
                      :label="item"
                      :value="item">
                  </el-option>
                </el-select>
              </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="12">
              <el-form-item label="单据日期:" prop="billdate">
                <el-date-picker
                    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="rebate_enddate">
                <el-date-picker
                    v-model="form.rebate_enddate"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择日期">
                </el-date-picker>
              </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="dialogFormVisible = false" 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: "edit",
  props:["data"],
  data() {
    return {
      dialogFormVisible:false,
      enterpriseShow:false,
      form:{
        "sa_cashbillid": 0,
        "sys_enterpriseid": 2, //合作企业档案ID
        "enterprisename":'',
        "remarks": "",
        "subclass": "",//明细分类
        "amount": "", //金额
        "rebate_enddate": '', //返利金有效期
        "billdate": '' //单据日期
      },
      rules:{
        enterprisename:[
          { required: true, message: '请选择企业', trigger: 'change'},
        ],
        subclass:[
          { required: true, message: '请选择分类明细', trigger: 'change'},
        ],
        amount:[
          { required: true, message: '请输入金额', trigger: 'blur'},
        ],
        rebate_enddate:[
          { 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,
      }
    }
  },
  methods:{
    onSubmit(){
      console.log(this.form)
      this.$refs.form.validate(async(valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": 20230216162402,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.dialogFormVisible = false
          this.$refs.form.resetFields()
        })
      })
    },
    onShow(){
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
      this.subClassList()
    },
    /*获取分类明细*/
    subClassList(){
      this.$store.dispatch('optiontypeselect','rebatebillpaytype').then(res=>{
        console.log(res,'分类明细')
        this.subClassData = res.data[0].subvalues
      })
    },
    /*获取企业*/
    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()
    },
  }
}
</script>

<style scoped>

</style>