<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">


        <a-form-item label="服务商" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <template v-if="!model.id">
            <a-input-search
              placeholder="选择服务商"
              readOnly
              v-decorator="[ 'hlwSubcontractorName', validatorRules.subcontractorId]"
              @search="onSearch">
              <a-button slot="enterButton" icon="search" v-if="!model.id">选择</a-button>
            </a-input-search>
          </template>
          <template v-else>
            <a-input v-decorator="[ 'hlwSubcontractorName', {rules: [{ required: true, message: '请选择服务商' }],initialValue:model.hlwSubcontractorName}]"  :disabled="true"></a-input>
          </template>
        </a-form-item>


        <a-form-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['bankName', validatorRules.bankName]" placeholder="请输入开户银行"></a-input>
        </a-form-item>

        <a-form-item label="银行账户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['bankAccount', validatorRules.bankAccount]" placeholder="请输入银行账户"></a-input>
        </a-form-item>


      </a-form>
    </a-spin>
  <subcontractor-select-window ref="subcontractorWindow" @ok="modalFormOk"></subcontractor-select-window>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import SubcontractorSelectWindow from '../../hlwsubcontractor/modules/SubcontractorSelectWindow'


  export default {
    name: "HlwCpSubcontractorBankAccountModal",
    components: {
      SubcontractorSelectWindow,
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        selectedDepartKeys:[], //保存用户选择服务商id
        checkedDepartKeys:[],
        confirmLoading: false,
        validatorRules: {
          subcontractorId: {
            rules: [
              { required: true, message: '请选择服务商!'},
            ]
          },
          bankName: {
            rules: [
              { required: true, message: '请输入开户银行!'},
            ]
          },
          bankAccount: {
            rules: [
              { required: true, message: '请输入银行账户!'},
            ]
          },
          createTime: {
            rules: [
              { required: true, message: '请输入创建时间!'},
            ]
          },
          createBy: {
            rules: [
              { required: true, message: '请输入创建者!'},
            ]
          },
        },
        url: {
          add: "/wisdom/hlwCpSubcontractorBankAccount/add",
          edit: "/wisdom/hlwCpSubcontractorBankAccount/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form =this.$form.createForm(this)
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'cpId','subcontractorId','bankName','bankAccount','createTime','createBy','updateTime','updateBy','hlwSubcontractorName'))
          this.form.setFieldsValue({ hlwSubcontractorName:this.model.hlwSubcontractorName!=null? this.model.hlwSubcontractorName:this.model.cpName})
        })
        this.selectedDepartKeys = [];
        this.checkedDepartKeys = [];
        if(this.model.subcontractorId){
          this.checkedDepartKeys.push(this.model.subcontractorId);
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.selectedDepartKeys = [];
        this.checkedDepartKeys = [];
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            if(this.checkedDepartKeys[0]){
              formData.subcontractorId = this.checkedDepartKeys[0];
            }
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'cpId','subcontractorId','bankName','bankAccount','createTime','createBy','updateTime','updateBy'))
      },

      //搜索服务商
      onSearch(){
        this.$refs.subcontractorWindow.add(this.checkedDepartKeys);
      },
      // 获取用户选择服务商弹提交的返回数据
      modalFormOk (formData) {
        this.selectedDepartKeys = [];
        for (let i = 0; i < formData.departIdList.length; i++) {
          this.selectedDepartKeys.push(formData.departIdList[i].key);
          this.form.setFieldsValue({ hlwSubcontractorName: formData.departIdList[i].title })
        }
        this.checkedDepartKeys = this.selectedDepartKeys  //更新当前的选择keys
      },

    }
  }
</script>