<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="客户全称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerName">
              <a-input v-model="model.customerName" placeholder="请输入客户全称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerShortName">
              <a-input v-model="model.customerShortName" placeholder="请输入客户简称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="系统类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="systemType">
<!--              <a-input v-model="model.systemType" placeholder="请输入系统类型"  ></a-input>-->
              <a-select
                      v-model="model.systemType"
                      placeholder="请选择系统类型"
                      :allowClear="true"
              >
                <a-select-option :value="item.id" v-for="(item,index) in systemTypeList" :key="index">{{ item.itemText }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerNo">
              <a-input v-model="model.customerNo" placeholder="请输入客户编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="主联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsName">
              <a-input v-model="model.contactsName" placeholder="请输入主联系人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerType">
<!--              <a-input v-model="model.customerType" placeholder="请输入客户类型"  ></a-input>-->
              <a-select
                      v-model="model.customerType"
                      placeholder="请选择客户类型"
                      :allowClear="true"
              >
                <a-select-option :value="item.id" v-for="(item,index) in customerTypeList" :key="index">{{ item.itemText }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="销售人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sellerId">
<!--              <a-input v-model="model.sellerId" placeholder="请输入销售人员"  ></a-input>-->
              <a-select
                      v-model="model.sellerId"
                      placeholder="请选择销售人员"
                      :allowClear="true"
              >
                <a-select-option :value="item.id" v-for="(item,index) in saleList" :key="index">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入手机号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
<!--              <a-input v-model="model.status" placeholder="请输入客户状态"  ></a-input>-->
              <a-select
                      v-model="model.status"
                      placeholder="请选择客户状态"
                      :allowClear="true"
              >
                <a-select-option :value="item.id" v-for="(item,index) in customerStatusList" :key="index">{{ item.itemText }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="region">
              <a-input v-model="model.region" placeholder="请输入地区"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postalCode">
              <a-input v-model="model.postalCode" placeholder="请输入邮编"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公司电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyPhone">
              <a-input v-model="model.companyPhone" placeholder="请输入公司电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="传真" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fax">
              <a-input v-model="model.fax" placeholder="请输入传真"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerSource">
<!--              <a-input v-model="model.customerSource" placeholder="请输入客户来源"  ></a-input>-->
              <a-select
                      v-model="model.customerSource"
                      placeholder="请选择客户来源"
                      :allowClear="true"
              >
                <a-select-option :value="item.id" v-for="(item,index) in customerSourceList" :key="index">{{ item.itemText }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="主页" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="homepage">
              <a-input v-model="model.homepage" placeholder="请输入主页"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Email" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入Email"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户行业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerIndustry">
<!--              <a-input v-model="model.customerIndustry" placeholder="请输入客户行业"  ></a-input>-->
              <a-select
                      v-model="model.customerIndustry"
                      placeholder="请选择客户行业"
                      :allowClear="true"
              >
                <a-select-option :value="item.id" v-for="(item,index) in customerIndustryList" :key="index">{{ item.itemText }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BusAgreementUnitForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
            customerName: [
              { required: true, message: '请输入客户全称!'},
           ],
            customerShortName: [
                { required: true, message: '请输入客户简称!'},
            ],
            systemType: [
                { required: true, message: '请选择系统类型!'},
            ],
            customerNo: [
                { required: true, message: '请输入客户编号!'},
            ],
            contactsName: [
                { required: true, message: '请输入主联系人!'},
            ],
            customerType: [
                { required: true, message: '请选择客户类型!'},
            ],
            sellerId: [
                { required: true, message: '请选择销售人员!'},
            ],
            phone: [
                { required: true, message: '请输入手机号!'},
            ],
            status: [
                { required: true, message: '请选择状态!'},
            ],
            customerSource: [
                { required: true, message: '请选择客户来源!'},
            ],
            customerIndustry: [
                { required: true, message: '请选择客户行业!'},
            ],
        },
        url: {
          add: "/business/busMarketAgreementUnit/add",
          edit: "/business/busMarketAgreementUnit/edit",
          queryById: "/business/busMarketAgreementUnit/queryById",
          query_saleList: "/business/busSalesPerson/queryList",
        },
        saleList:[],
        systemTypeList:[],
        customerTypeList:[],
        customerStatusList:[],
        customerSourceList:[],
        customerIndustryList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      getAction(this.url.query_saleList,{}).then((res)=>{
        if(res.success){
          this.saleList = res.result
        }else{

        }
      })
      this.getDictConfig();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              var info = JSON.parse(localStorage.getItem("storeInfo"));
              this.model.hotelId = info.id;
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      getDictConfig(){
        var that = this;
        //系统类型
        var param = {
          dictName:'协议系统类型设置'
        }
        // var values = JSON.parse(JSON.stringify(param))
        that.confirmLoading = true;
        getAction('/business/busDictItem/queryList',param).then((res)=>{
          console.log(res)
          if(res.success){
            if (res.code == 200 && res.result) {
              this.systemTypeList = res.result;
            }
          }
        }).finally(() => {
          that.confirmLoading = false;
        })

        //客户类型
        var param = {
          dictName:'协议客户类型设置'
        }
        // var values = JSON.parse(JSON.stringify(param))
        that.confirmLoading = true;
        getAction('/business/busDictItem/queryList',param).then((res)=>{
          console.log(res)
          if(res.success){
            if (res.code == 200 && res.result) {
              this.customerTypeList = res.result;
            }
          }
        }).finally(() => {
          that.confirmLoading = false;
        })

        //客户状态
        var param = {
          dictName:'协议客户状态设置'
        }
        // var values = JSON.parse(JSON.stringify(param))
        that.confirmLoading = true;
        getAction('/business/busDictItem/queryList',param).then((res)=>{
          console.log(res)
          if(res.success){
            if (res.code == 200 && res.result) {
              this.customerStatusList = res.result;
            }
          }
        }).finally(() => {
          that.confirmLoading = false;
        })

        //客户来源
        var param = {
          dictName:'客人来源设置'
        }
        // var values = JSON.parse(JSON.stringify(param))
        that.confirmLoading = true;
        getAction('/business/busDictItem/queryList',param).then((res)=>{
          console.log(res)
          if(res.success){
            if (res.code == 200 && res.result) {
              this.customerSourceList = res.result;
            }
          }
        }).finally(() => {
          that.confirmLoading = false;
        })

        //客户行业
        var param = {
          dictName:'协议客户行业设置'
        }
        // var values = JSON.parse(JSON.stringify(param))
        that.confirmLoading = true;
        getAction('/business/busDictItem/queryList',param).then((res)=>{
          console.log(res)
          if(res.success){
            if (res.code == 200 && res.result) {
              this.customerIndustryList = res.result;
            }
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }
    }
  }
</script>
