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

        <a-form-item label="盘点日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择盘点日期" v-decorator="[ 'checkDate', validatorRules.checkDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="资产编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请选择" v-decorator="[ 'assetEncoding', validatorRules.assetEncoding]" @change="handleChange" >
            <!-- <a-select-option :value="undefined">请选择</a-select-option> -->
            <a-select-option
              v-for="(item,index) in assets"
              :key="index"
              :value="item.assetNumber">
              {{ item.assetNumber }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="资产名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'assetName', validatorRules.assetName]" placeholder="请输入资产名称"  ></a-input>
        </a-form-item>
        <a-form-item label="资产类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'assetsCategory', validatorRules.assetsCategory]" placeholder="请输入资产类别" disabled></a-input>
        </a-form-item>
        <a-form-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'specificationsModels', validatorRules.specificationsModels]" placeholder="请输入规格型号" disabled></a-input>
        </a-form-item>
        <a-form-item label="原值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'originalValue', validatorRules.originalValue]" placeholder="请输入原值" disabled></a-input>
        </a-form-item>
        <a-form-item label="购置日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'carAcquisitionDate', validatorRules.carAcquisitionDate]" placeholder="请输入购置日期" disabled></a-input>
        </a-form-item>
        <a-form-item label="使用年限" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'durableYears', validatorRules.durableYears]" placeholder="请输入使用年限" disabled></a-input>
        </a-form-item>
        <a-form-item label="存放地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'storageLocation', validatorRules.storageLocation]" placeholder="请输入存放地点"></a-input>
        </a-form-item>
        <a-form-item label="使用人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-select-user-by-dep v-decorator="['usePersonnel', validatorRules.usePersonnel]" :trigger-change="true" />
        </a-form-item>
        <a-form-item label="使用部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-select-depart v-decorator="['useDepartment', validatorRules.useDepartment]" :trigger-change="true" customReturnField="departName"/>
        </a-form-item>
        <a-form-item label="资产状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['assetState', validatorRules.assetState]" :trigger-change="true" dictCode="assets_status" placeholder="请选择资产状态"/>
        </a-form-item>
        <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['accessory', validatorRules.accessory]" :trigger-change="true"></j-upload>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JUpload from '@/components/jeecg/JUpload'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import moment from 'moment'

  export default {
    name: "InventoryRecordsModal",
    components: { 
      JDate,
      JUpload,
      JSelectDepart,
      JSelectUserByDep,
      JDictSelectTag,
    },
    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 },
        },
        confirmLoading: false,
        validatorRules: {
          checkDate: {rules: [
          ]},
          assetEncoding: {rules: [
          ]},
          assetName: {rules: [
          ]},
          assetsCategory: {rules: [
          ]},
          specificationsModels: {rules: [
          ]},
          originalValue: {rules: [
          ]},
          carAcquisitionDate: {rules: [
          ]},
          durableYears: {rules: [
          ]},
          storageLocation: {rules: [
          ]},
          usePersonnel: {rules: [
          ]},
          useDepartment: {rules: [
          ]},
          assetState: {rules: [
          ]},
          accessory: {rules: [
          ]},
        },
        url: {
          add: "/oa/inventoryRecords/add",
          edit: "/oa/inventoryRecords/edit",
          allAssets: "/oa/assets/all",
        },
        assets: []
      }
    },
    mounted () {
      this.initAssets()
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'checkDate','assetEncoding','assetName','assetsCategory','specificationsModels','originalValue','carAcquisitionDate','durableYears','storageLocation','usePersonnel','useDepartment','assetState','accessory'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      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);
            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,'checkDate','assetEncoding','assetName','assetsCategory','specificationsModels','originalValue','carAcquisitionDate','durableYears','storageLocation','usePersonnel','useDepartment','assetState','accessory'))
      },
      initAssets(){
        getAction(this.url.allAssets).then((res)=>{
          if(res.success){
            this.assets = res.result
          }else {
            this.$message.warning(res.message)
          }
        })
      },
      handleChange(val,op){
        var asset = this.assets.find(i=>{
          return i.assetNumber===val
        })
        console.log(asset)
        let durableYears  =  moment(asset.acquisitionDate,"YYYY-MM-DD").diff(moment(),"year")
        this.form.setFieldsValue({
          assetName:asset.assetName,
          assetsCategory:asset.assetsCategory,
          specificationsModels:asset.specificationsModels,
          originalValue:asset.unitPrice,
          carAcquisitionDate:asset.acquisitionDate,
          durableYears:durableYears,
        })
        
      }

      
    }
  }
</script>