<template>
  <div>
    <el-button size="mini" type="primary" @click="onShow">编 辑</el-button>
    <el-drawer title="新建档案" direction="rtl" append-to-body :visible.sync="dialogFormVisible" :show-close="false" size="600px">
      <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="20">
              <el-form-item label="企业名称" prop="enterprisename">
                <el-input v-model="form.enterprisename" placeholder="输入企业名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-popover
                  placement="right"
                  width="900"
                  v-model="businessShow"
                  trigger="manual">
                <el-input
                    style="width:300px;margin-bottom: 10px"
                    placeholder="请输入内容"
                    v-model="form.enterprisename"
                    clearable
                    @clear="businessSearch"
                    size="mini"
                    @keyup.enter.native="businessSearch">
                  <i slot="prefix" class="el-icon-search" @click="businessSearch"></i>
                </el-input>
                <el-table :data="businessData" @row-click="businessSelect" :header-cell-style="{background:'#EEEEEE',color:'#333'}" size="mini">
                  <el-table-column width="260" label="客户名称">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.companyName?scope.row.companyName:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column  label="客户地址">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.address?scope.row.address:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column width="200"  label="客户税号">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.taxNum?scope.row.taxNum:'--' }}</span>
                    </template>
                  </el-table-column>
                </el-table>
                <el-button size="mini" type="primary" style="width: 100%" @click="businessSearch" slot="reference" :disabled="form.enterprisename === ''">工商查询</el-button>
              </el-popover>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人" prop="contact">
                <el-input v-model="form.contact" placeholder="输入企业联系人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input v-model="form.phonenumber" placeholder="输入联系电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="省市县" prop="province">
                <el-cascader
                  style="width:100%"
                  v-model="form.province"
                  :options="basicData.data().areaData"
                  @change="cascaderChange" clearable>
                </el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="税号" prop="taxno">
                <el-input v-model="form.taxno" placeholder="输入税号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否免运费" prop="freefreightamount">
                <el-radio-group v-model="radio" @change="radioChange">
                  <el-radio :label="-1">不免费运费</el-radio>
                  <el-radio :label="0">免运费</el-radio>
                  <el-radio :label="1">其他</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="radio === 1">
              <el-form-item label="免运费额度" prop="freefreightamount">
                <el-input v-model.number="form.freefreightamount" placeholder="请输入免运费额度"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="radio === 1?12:24">
              <el-form-item label="可退货天数" prop="limitreturnday">
                <el-input v-model.number="form.limitreturnday" placeholder="请输入订单可退货天数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票节点" prop="invoicingpoint">
                <el-radio-group v-model="form.invoicingpoint">
                  <el-radio :label="1">订单审核</el-radio>
                  <el-radio :label="2">发货</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="企业等级">
                <el-select style="width:100%" v-model="form.grade" placeholder="请选择">
                  <el-option
                    v-for="item in agent_level"
                    :key="item.value"
                    :label="item.remarks"
                    :value="item.value">
                    <span style="float: left">{{ item.value }}</span>
                    <span style="float: right; color: #8492a6; font-size: 12px">{{ item.remarks?item.remarks:'暂无描述' }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否授权所有营销类别:" prop="saleclassauth" label-width="160px">
                <el-checkbox v-model="form.saleclassauth" :true-label="0" :false-label="1"></el-checkbox>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="erp编号" prop="erpagentnum">
                <el-input v-model="form.erpagentnum" placeholder="输入erp编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="详细地址" prop="address">
                <el-input v-model="form.address" type="textarea" :rows="3" 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="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
export default {
  props:['data'],
  data () {
    return {
      radio:-1,
      dialogFormVisible:false,
      businessData:[],
      businessParam:{
        "id": 20221208103601,
        "content": {
          "pageNumber":1,
          "pageSize":5,
          "keyword":''
        },
      },
      businessShow:false,
      total:0,
      currentPage:0,
      form:{
        "address": "",
        "city": "",
        "changeby": "",
        "county": "",
        "phonenumber": "",
        "sys_enterpriseid": 0,
        "enterprisename": "",
        "province": "",
        "contact": "",
        "taxno": "",
        "limitreturnday": "", //订单可退货天数
        "freefreightamount": "", //免运费额度
        "latitude": "", //纬度
        "longitude": "", //经度
        "invoicingpoint": 1, //开票节点（1:订单审核：2:发货）
        "saleclassauth": 1 //是否需要经营授权,默认1
      },
      agent_level:[],
      rules:{
        enterprisename: [
          { required: true, message: '输入企业名称', trigger: 'blur' },
        ],
        contact: [
          { required: true, message: '输入企业联系人', trigger: 'blur' },
        ],
        phonenumber: [
          { required: true, message: '输入联系电话', trigger: 'blur' },
        ],
        taxno: [
          { required: true, message: '输入税号', trigger: 'blur' },
        ],
        province: [
          { required: true, message: '选择省市县', trigger: 'change' },
        ],
        address:[
          { required: true, message: '输入详细地址', trigger: 'blur' },
        ],
        freefreightamount:[
          {type:'number',message:'请输入数字',trigger:'blur'}
        ],
        limitreturnday:[
          {type:'number',message:'请输入数字',trigger:'blur'}
        ],
      }
    }
  },
  computed:{
    ...mapGetters ({
      arealist:'arealist'
    })
  },
  methods:{
    onShow () {
      this.form = Object.assign({},this.form,this.data)
      if (this.form.province) {
        this.form.province = [this.form.province,this.form.city,this.form.county]
      }
      switch (this.form.freefreightamount) {
        case -1:
          this.radio = -1
          break;
        case 0:
          this.radio = 0
          break;
        default:
          this.radio = 1
          break;
      }
      this.dialogFormVisible = true
      this.query_sysoptions()
    },
    async query_sysoptions () {
      const res = await this.$store.dispatch('optiontypeselect','agentgrade')
      this.agent_level = res.data
    },
    cascaderChange (val) {
      this.form.province = val
      
    },
    onSubmit () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
          this.form.city = this.form.province[1]
          this.form.county = this.form.province[2]
          this.form.province = this.form.province[0]
          const res = await this.$api.requested({
          "id": 20220920084101,
          "content":this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
     })
    },
    /*工商查询*/
    async businessSearch(){
      this.businessParam.content.keyword = this.form.enterprisename
      const res = await this.$api.requested(this.businessParam)
      console.log(res,"工商查询结果")
      this.businessData = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
      this.businessShow = true
    },
    /*选择工商信息*/
    businessSelect(val){
      console.log("选择的工商信息",val)
      this.$confirm('是否以查询的工商信息更新客户信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.businessShow = false
        this.form.enterprisename = val.companyName
        this.form.address = val.address
        this.form.taxno = val.taxNum
        /*this.value = [val.regProvince,val.regCity,val.regArea]*/
        this.form.province = [val.regProvince,val.regCity,val.regArea]
        this.form.city = val.regCity
        this.form.county = val.regArea
        this.form.contact = val.legalPerson
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消更新'
        });
      })
    },
    radioChange (val) {
      switch (val) {
        case -1:
          this.form.freefreightamount = -1
          break;
        case 0:
          this.form.freefreightamount = 0
          break;
        default:
          this.form.freefreightamount = ''
          break;
      }
    }
  },
  mounted () {
  }
}

</script>
<style>
</style>