<template>
  <div>
    <el-button type="primary" size="small" @click="handleCommand">新 建</el-button>
    <el-dialog
      title="新建借用单"
      :visible.sync="drawer"
      direction="rtl"
      :show-close="false"
      append-to-body
      width="400px"
      @close="refresh">
      <div>
        <el-row :gutter="15">
          <el-form size="small" ref="form" :model="form" :rules="rules" label-width="100px" :status-icon="false">
            <el-col :span="24">
              <el-form-item label="借用方式">
               <el-radio-group v-model="radio">
                  <el-radio :label="3">按合同约定借用</el-radio>
                  <el-radio :label="6">单个借用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col v-if="radio === 3" :span="24">
              <el-form-item label="工具合同">
                <el-popover
                  placement="bottom"
                  trigger="click"
                  v-model="visible">
                  <toolCanUseContract ref="contract" @rowClick="contractClick"></toolCanUseContract>
                  <el-input readonly v-model="form.contractname" slot="reference" placeholder="选择合同"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="submitForm">创 建</el-button>
        <el-button size="small" @click="refresh">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import enterprise from '@/template/enterprise/index.vue'
import toolCanUseContract from '@/template/toolBorrowingCanUseContract/index.vue'

export default {
  data () {
    return {
      drawer:false,
      visible:false,
      visible1:false,
      radio:3,
      form:{
        "sa_orderid": 0,
        "sys_enterpriseid": 0, //订货企业id，可不传，默认取当前账号的
        "sa_accountclassid": 0, //营销账户类型ID
        "sa_brandid": "", //品牌ID
        "sa_contractid": null, //合同ID，标准订单不传
        "sa_projectid": 0, //工程项目表ID，标准订单不传
        "sys_enterprise_financeid": 0, //合作企业财务信息ID（开票信息）
        "sa_logiscompid": 0, //物流公司档案ID
        "rec_contactsid": 0, //合作企业联系人表ID（收货信息）
        "type": "", //订单类型
        "typemx": "", //明细分类，可选
        "remarks": "", //可选
        "tradefield":"",//必选
        "pay_enterpriseid": 0, //结算单位
        "rebate_used":0,//默认0，是否使用返利金
        "billdate":"",//单据日期，默认创建日期
        "signbacknum":"",
        "promname":""
      },
      rules: {
        sa_brandid: [{
          required: true,
          message: '请选择选择品牌',
          trigger: 'change'
        }],
        tradefield: [{
          required: true,
          message: '请选择选择领域',
          trigger: 'change'
        }],
        typemx: [{
          required: true,
          message: '请选择选子分类',
          trigger: 'change'
        }],
        sys_enterpriseid:[{
          required: true,
          message: '订货企业不能为空',
          trigger: 'blur'
        }],
        promname:[{
          required: true,
          message: '活动不能为空',
          trigger: 'blur'
        }],
      },
      sa_brandidOptions: [],
      tradefieldOptions: [],
      typemxlist:[],
      childField:[]
    }
  },
  components:{
    enterprise,
    toolCanUseContract
  },
  methods:{
    handleCommand (command) {
      this.drawer = true
      this.pageData()
      this.specordermx()
      this.$nextTick(()=>{
        this.$refs['contract'].listData()
      })
      
    },
    submitForm() {
      this.$refs['form'].validate(async valid => {
        if (!valid) return
        const res = await this.$api.requested({
          "id": 20230116092702,
          "content":this.form
        })
        this.tool.showMessage(res,()=>{
          if (this.form.sa_contractid) {
            this.addContractTool(res.data.sa_orderid)
          }

          this.drawer = false
         
          this.$store.dispatch('changeDetailDrawer',true)
          this.$router.push({path:'/toolBorrowingDetail',query:{id:res.data.sa_orderid,rowindex:res.data.rowindex}})
        })
      })
    },
    async addContractTool (sa_orderid) {
      const res = await this.$api.requested({
        "id": 20230116142602,
        "content": {
          "sa_orderid":sa_orderid,
          "sys_enterpriseid": this.form.sys_enterpriseid,
          "sa_contractid":this.form.sa_contractid
        },
      })
    },
    async specordermx () {
      const res = await this.$store.dispatch('optiontypeselect','specordermx')
      this.typemxlist = res.data
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    async pageData () {
      const res = await this.$api.requested({
        "id": 20220924163702,
        "content": {
          "pageSize": 999,
        }
      })
      this.sa_brandidOptions = res.data

      const res1 = await this.$api.requested({
        "id": 20221223141802,
        "content": {
          "pageSize": 999,
        }
      })
      this.tradefieldOptions = res1.data
    },
    // rowClick (row) {
    //   this.form.sys_enterpriseid = row.sys_enterpriseid
    //   this.$refs['contract'].listData(row.sys_enterpriseid)
    // },
    contractClick (row) {
      this.form.sa_contractid = row.sa_contractid
      this.form.sa_projectid = row.sa_projectid
      this.form.contractname = row.title === ''?'未知':row.title
      this.visible = false
    },
    setChildField (item) {
      this.childField = item.subvalues
    },
    refresh () {
      this.form = {
        "sa_orderid": 0,
        "sys_enterpriseid": 0, //订货企业id，可不传，默认取当前账号的
        "sa_accountclassid": 0, //营销账户类型ID
        "sa_brandid": "", //品牌ID
        "sa_contractid": 0, //合同ID，标准订单不传
        "sa_projectid": 0, //工程项目表ID，标准订单不传
        "sys_enterprise_financeid": 0, //合作企业财务信息ID（开票信息）
        "sa_logiscompid": 0, //物流公司档案ID
        "rec_contactsid": 0, //合作企业联系人表ID（收货信息）
        "type": "", //订单类型
        "typemx": "", //明细分类，可选
        "remarks": "", //可选
        "saler_hrid":0,//销售人员hrid,业务员hrid
        "tradefield":"",//必选
        "pay_enterpriseid": 0, //结算单位
        "rebate_used":0,//默认0，是否使用返利金
        "billdate":"",//单据日期，默认创建日期
        "signbacknum":""
      }
      this.drawer = false
    }
  }
}

</script>
<style>
</style>