<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <j-dict-select-tag v-decorator="['deviceId',validatorRules.deviceId]" :trigger-change="true" placeholder="请选择设备id"-->
<!--                                 dictCode="bu_device,device_name,id"/>-->
              <j-select-device v-devorator="['deviceId',{}]" :trigger-change="true" plactholder="请选择设备名称"
                               dictCode="bu_device,device_name,id" ></j-select-device>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['deviceCode']" placeholder="请输入设备编号" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="购买时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择购买时间" v-decorator="['buyTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="购买说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['buyDes']" placeholder="请输入购买说明" :rows="2" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="购置操作人" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['buyOperatorId']" placeholder="请选择购置操作人"  ></a-input>-->
              <j-select-user-by-dep v-model="buyOperatorId" :multi="false" placeholder="请选择购置操作人" ></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="封存日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择封存日期" v-decorator="['sealUpTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="封存原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sealUpReason']" placeholder="请输入封存原因"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="封存操作人" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['sealUpOperatorId']" placeholder="请选择封存操作人"  ></a-input>-->
              <j-select-user-by-dep v-model="sealUpOperatorId" placeholder="请选择封存操作人" ></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="启封日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择启封日期" v-decorator="['unsealUpTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="启封原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unsealUpReason']" placeholder="请输入启封原因"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="启封操作人" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['unsealUpOperatorId']" placeholder="请选择启封操作人"  ></a-input>-->
              <j-select-user-by-dep v-model="unsealUpOperatorId" placeholder="请选择购置操作人" ></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="报废时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择报废时间" v-decorator="['scrapTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="报废原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['scrapReason']" placeholder="请输入报废原因"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="报废操作人" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['scrapOperatorId']" placeholder="请选择报废操作人"  ></a-input>-->
              <j-select-user-by-dep v-model="scrapOperatorId" placeholder="请选择购置操作人" ></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JSelectUserByDep from '@comp/jeecgbiz/JSelectUserByDep'
  import JSelectDevice from '@comp/jeecgbiz/JSelectDevice'

  export default {
    name: 'BuDeviceLifeCircleForm',
    components: {
      JSelectDevice,
      JSelectUserByDep,
      JFormContainer,
      JDate,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/device/buDeviceLifeCircle/add",
          edit: "/device/buDeviceLifeCircle/edit",
          queryById: "/device/buDeviceLifeCircle/queryById"
        },
        buyOperatorId:"",
        sealUpOperatorId:"",
        unsealUpOperatorId:"",
        scrapOperatorId:"",
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        console.log(record);
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.buyOperatorId = record.buyOperatorId;
        this.sealUpOperatorId = record.sealUpOperatorId;
        this.unsealUpOperatorId = record.unsealUpOperatorId;
        this.scrapOperatorId = record.scrapOperatorId;
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'deviceId','buyTime','buyDes','sealUpTime','sealUpReason','unsealUpTime','unsealUpReason','','scrapTime','scrapReason'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        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);
            formData.buyOperatorId = this.buyOperatorId;
            formData.sealUpOperatorId = this.sealUpOperatorId;
            formData.unsealUpOperatorId = this.unsealUpOperatorId;
            formData.scrapOperatorId = this.scrapOperatorId;
            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;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'deviceId','buyTime','buyDes','buyOperatorId','sealUpTime','sealUpReason','sealUpOperatorId','unsealUpTime','unsealUpReason','unsealUpOperatorId','scrapTime','scrapReason','scrapOperatorId'))
      },
    }
  }
</script>