<template>
  <a-modal
    :title="title"
    :width="width"
    :bodyStyle="bodyStyle"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :footer="null"
    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="选择服务商"
              disabled
              v-decorator="[ 'subcontractorName', 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="[ 'subcontractorName', validatorRules.subcontractorId]"  :disabled="true"></a-input>
          </template>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-button class="ant-btn"  @click="handleCancel">取消</a-button>
      <a-button class="ant-btn ant-btn-primary" style="margin-left: 8px"  @click="handleOk">确定</a-button>
    </div>
    <subcontractor-window ref="subcontractorWindow" @ok="modalFormOk"></subcontractor-window>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  // 引入搜索服务商弹出框的组件
  import subcontractorWindow from './SubcontractorWindow'
  
  export default {
    name: "HlgPlatformSubcontractorModal",
    components: {
      subcontractorWindow
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        bodyStyle: {
          paddingTop: '50px',
          paddingBottom:'50px',
          height: (window.innerHeight * 0.30) + 'px',
          'overflow-y': 'auto'
        },
        disableSubmit:false,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        selectedDepartKeys:[], //保存用户选择服务商id
        checkedDepartKeys:[],
        confirmLoading: false,
        platformId: 0,
        validatorRules:{
          subcontractorId:{rules: [{ required: true, message: '请输入服务商!' }]},
        },
        url: {
          add: "/hlgplatform/hlgPlatformSubcontractor/add",
          edit: "/hlgplatform/hlgPlatformSubcontractor/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add (platformId) {
        this.edit(platformId);
      },
      edit (platformId) {
        this.form.resetFields();
        this.platformId = platformId;
        this.visible = true;
        this.selectedDepartKeys = [];
        this.checkedDepartKeys = [];
      },
      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.selectedDepartKeys[0]){
              formData.subcontractorId = this.selectedDepartKeys[0];
            }
            formData.platformId = this.platformId;
            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();
            })
          }
         
        })
      },
      //搜索服务商
      onSearch(){
        this.$refs.subcontractorWindow.add(this.checkedDepartKeys);
      },
      handleCancel () {
        this.close()
      },
      // 获取用户选择服务商弹提交的返回数据
      modalFormOk (formData) {
        this.selectedDepartKeys = [];
        for (let i = 0; i < formData.departIdList.length; i++) {
          this.selectedDepartKeys.push(formData.departIdList[i].key);
          this.form.setFieldsValue({ subcontractorName: formData.departIdList[i].title })
        }
        this.checkedDepartKeys = this.selectedDepartKeys  //更新当前的选择keys
      },
      
    }
  }
</script>
<style>
  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
  .avatar-uploader > .ant-upload {
    width: 104px;
    height: 104px;
  }

  .ant-upload-select-picture-card i {
    font-size: 49px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
</style>