<!--
 * @Description: 项目经理（集团外用户）-添加行项目信息
 * @Author:lxl
 * @Date: 2020-07-10 13.:13:30
-->
<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="dialogData.consignContractVisible"
      :close-on-click-modal="false"
      append-to-body
      :lock-scroll="false"
      title="录入行项目信息"
      width="1186px"
      custom-class="el-dialog__body-p8 el-dialog_table"
      @close="closeOff"
    >
      <div class="p0-16">
        <el-form
          ref="formData"
          :model="formData"
          :rules="rules"
          :inline-message="true"
        >
          <table class="from_table">
            <tr>
              <td> <el-form-item label="采购申请编号" prop="purchasePlanNo" /></td>
              <td>{{ dialogData.purchasePlanNo }}</td>
              <td>
                <el-form-item label="行项目编码" prop="purchaseNo" />
              </td>
              <td>
                <gx-input
                  v-model="formData.purchaseNo"
                  show-word-limit
                  gx-maxlength="varchar200"
                  :disabled="true"
                />
              </td>
            </tr>
            <tr>
              <td> <el-form-item label="采购计划名称" prop="purchasePlanName" /></td>
              <td>{{ dialogData.purchasePlanName }}</td>
              <td>
                <el-form-item label="物资编码" prop="purchaseCode" />
              </td>
              <td>
                <gx-input
                  v-model="formData.purchaseCode"
                  show-word-limit
                  gx-maxlength="varchar50"
                  style="width: calc(100% - 156px );"
                  :disabled="true"
                />
                <el-button type="primary" @click="dialogSonData.dialogVisibleGoogs=true">
                  选择
                </el-button>
                <el-button plain @click="onPurchaseClear">
                  清空
                </el-button>
              </td>
            </tr>
            <tr>
              <td>
                <el-form-item label="一级专业分类" prop="purchaseClassificationParent" />
              </td>
              <td>
                <span>{{ formData.purchaseClassificationParent }}</span>
              </td>
              <td>
                <el-form-item label="二级专业分类" prop="purchaseClassification" />
              </td>
              <td>
                <span>{{ formData.purchaseClassification }}</span>
              </td>
            </tr>
            <tr>
              <td> <el-form-item label="物资名称" prop="purchaseName" /></td>
              <td>
                <gx-input
                  v-model="formData.purchaseName"
                  show-word-limit
                  gx-maxlength="varchar50"
                  :disabled="true"
                />
              </td>
              <td> <el-form-item label="物资规格说明" prop="purchaseSpecification" /></td>
              <td>
                <gx-input
                  v-model="formData.purchaseSpecification"
                  :disabled="true"
                  show-word-limit
                />
              </td>
            </tr>
            <tr>
              <!--              <td><el-form-item label="执行结果编号" /></td>-->
              <!--              <td>{{ formData.resultExeNo }}</td>-->
              <td> <el-form-item label="品牌" /></td>
              <td colspan="3">
                <span>{{ formData.purchaseBrand }}</span>
              </td>
            </tr>
            <tr>
              <td> <el-form-item ref="purchaseNum" label="数量" prop="purchaseNum" /></td>
              <td>
                <money :thousands="true" :unit-fixed="4" :width="'279px'" :money-value="formData" name="purchaseNum" />
              </td>
              <td>
                <el-form-item label="计量单位" prop="purchaseUnit" />
              </td>
              <td>
                <el-select v-model="formData.purchaseUnit" filterable placeholder="请选择" style="width: 104%">
                  <el-option
                    v-for="item in purchaseUnitList"
                    :key="item.typecode"
                    :label="item.typename"
                    :value="item.typecode"
                  />
                </el-select>
              </td>
            </tr>
            <tr>
              <td> <el-form-item label="预算净单价(元)" prop="purchaseBudget" /></td>
              <td>
                <!--金额组件-->
                <money :thousands="true" :width="'278px'" :money-value="formData" name="purchaseBudget" />
              </td>
              <td> <el-form-item label="预算净总价(元)" prop="purchaseTotalBudget" /></td>
              <td>
                <!--金额组件-->
                <money :thousands="true" :disabled="true" :width="'286px'" :money-value="formData" name="purchaseTotalBudget" />
              </td>
            </tr>
            <tr>
              <td> <el-form-item label="税率(%)" prop="purchaseTaxRate" /></td>
              <td>
                <el-select v-model="formData.purchaseTaxRate" placeholder="请选择" width="260">
                  <el-option
                    v-for="item in taxRateList"
                    :key="item.typecode"
                    :label="item.typename"
                    :value="item.typecode"
                  />
                </el-select>
              </td>
              <td><el-form-item label="是否集采" /></td>
              <td>
                {{ filterDictText(collectionTypeList,formData.collectionType) }}
              </td>
            </tr>
            <tr>
              <td>
                <el-form-item label="技术联系人/电话" prop="planManagerName" />
              </td>
              <td>
                <gx-input
                  v-model="formData.planManagerName"
                  show-word-limit
                  gx-maxlength="varchar100"
                />
              </td>
              <td> <el-form-item ref="deliveryTime" label="交货日期" prop="deliveryTime" /></td>
              <td>
                <el-date-picker
                  v-model="formData.deliveryTime"
                  :picker-options="disabledPickerOptions"
                  :value-format="elValueFormatDate"
                  type="date"
                  placeholder="选择交货时间"
                  style="width: 93%"
                  maxlength="20"
                />
              </td>
            </tr>
            <tr>
              <td><el-form-item label="交货地点" prop="deliveryAddCompany" /></td>
              <td>
                {{ formData.deliveryAddCompany }}
              </td>
              <td>
                <el-form-item :label-position="labelposition" :inline-message="true" prop="depotId" label="库存地点" style="" />
              </td>
              <td>
                <el-select v-model="formData.depotId" style="" placeholder="库存地点">
                  <el-option
                    v-for="item in formData.depotList"
                    :key="item.depotId"
                    :label="item.depotSite"
                    :value="item.depotId"
                  />
                </el-select>
              </td>
            </tr>
            <tr>
              <td> <el-form-item label="备注" /></td>
              <td colspan="3">
                <gx-input
                  v-model="formData.remark"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                  type="textarea"
                  gx-maxlength="varchar4000"
                  show-word-limit
                />
              </td>
            </tr>
            <tr>
              <td><el-form-item label="附件" /></td>
              <td colspan="3">
                <!-- <upload
                  ref="upload"
                  :title="''"
                  :on-change="principalFile"
                  :data="lineItemFileType"
                  :file-list="principalList"
                  :auto-delete="true"
                  :auto-upload="true"
                  :limit="10"
                  :file-size="200"
                  tip-text="上传附件格式（*.doc;*.docx;*.pdf;*.png;*.jpg;*.zip;*.xlsx;*.xls;*.rar）单个文件不得超过200M"
                  accept=".doc,.docx,.pdf,.png,.jpg,.zip,.jpeg,.txt,.xls,.ppt,.pptx,.rar"
                  file-ref="fileRef"
                  but-text="点击上传"
                /> -->
                <uploadFile :file-data="lineItemFileType" />
              </td>
            </tr>
          </table>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="conformLoading" @click="okdialog('formData')">确 认</el-button>
        <el-button @click="closeOf">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 选择物料编码 -->
    <selGoogsDialog ref="heandlerClose" :dialog-son-data="dialogSonData" @onRes="onRes" />
    <!-- 选择计划负责人 -->
    <lineltemlnfoPrincipalDialog :dialog-take-data="dialogTakeData" @onTakeRes="onTakeRes" />
  </div>
</template>
<script>
/* api */
import { savePurchasePlanDetailInfo, queryMainPurchasePlanDetailInfo } from '@/api/procurement/purchasplan'// 采购平台录入行项目信息
/** 公共方法 */
import { restoreInitialValue } from '@/assets/js/utils' // 清空
import { filterDictText } from '@/assets/js/utils'// 常用方法
// import { procurementPlanGrounpMixins } from '@/views/procurement/procurementPlan/mixins/procurementPlanGroupMixins' // 公共字典
import { procurementMixins } from '@/views/procurement/mixins/procurementMixins'
import { checkNum, characters1_50, characters1_auto, checkMyriadMoney } from '@/assets/js/validate'// 校验输入内容
/** 公共组件 */
// import upload from '@comp/common/updownload/upload'// 文件上传组件
import money from '@comp/common/money/money' // 金币组件
import uploadFile from '@/views/procurement/procurementPlan/components/uploadFile' // 附件上传

/* 弹框 */
import selGoogsDialog from '@/views/procurement/procurementPlan/components/selGoogs-dialog.vue' // 选择物料编码
import lineltemlnfoPrincipalDialog from './lineltemlnfoPrincipal-dialog' // 选择计划负责人
export default {
  name: 'AddLineItemInfoDialog',
  components: {
    // upload, // 文件上传
    money, // 金额组件
    selGoogsDialog, // 选择无聊编码
    lineltemlnfoPrincipalDialog, // 选择计划负责人
    uploadFile // 附件上传
  },
  mixins: [procurementMixins],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return {}
      }
    },
    planmainid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // value: '',
      /* -----------数据字典start-------------*/
      taxRateList: [], // 税率
      purchaseUnitList: [], // 计量单位
      collectionTypeList: [], // 是否集采
      dictionaryType: ['taxRate', 'collectionType', 'purchaseUnit'],
      /* -----------数据字典end-------------*/
      dialogSonData: {
        dialogVisibleGoogs: false, // 选择物资编码
        collectionType: this.dialogData.collectionType // 采购组织形式
      },
      dialogTakeData: {
        dialogVisibleTake: false // 选择计划负责人
      },
      conformLoading: false,
      labelposition: 'right', // label位置
      formData: {
        deliveryAddCompany: '', // 交货地点
        deliveryAreaCode: '',
        purchaseTaxRate: '', // 税率
        operation: '00', // 保存
        purchaseUnit: '', // 计量单位
        depotId: '' // 库存地点id
      },
      purchaseData: {}, // 物资编码信息
      principalList: [], // 上传附件数据
      // 校验规则
      rules: {
        purchaseBudget: [
          { required: true, validator: characters1_auto, trigger: 'blur' }, // 单价
          { validator: checkMyriadMoney, trigger: 'blur' }
        ],
        purchaseNo: [{ required: true, validator: characters1_50, trigger: 'blur' }], // 行项目编码
        purchaseName: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 物资名称
        purchaseNum: [{ required: true, validator: checkNum, trigger: 'blur' }], // 数量
        purchaseUnit: [{ required: true, message: '请选择', trigger: 'change' }], // 计量单位
        purchaseTaxRate: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 税率
        deliveryTime: [{ required: true, message: '请选择', trigger: 'change' }], // 交货时间
        planManagerName: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 计划负责人
        purchaseSpecification: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 规格
        purchaseTotalBudget: [
          { required: true, validator: characters1_auto, trigger: 'blur' }, // 预算净单价
          { validator: checkMyriadMoney, trigger: 'blur' }
        ],
        deliveryAddCompany: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 交货地点
        depotId: [{ required: true, message: '请选择', trigger: 'change' }] // 库存地点
      }
    }
  },
  watch: {
    'formData.purchaseBudget'(val) {
      if (val) {
        if (this.formData.purchaseNum === null || this.formData.purchaseNum === '') {
          this.formData.purchaseTotalBudget = null
        } else {
          this.formData.purchaseTotalBudget = (val * this.formData.purchaseNum).toFixed(2)
        }
      } else {
        // 单价未填写 总价清空
        this.formData.purchaseTotalBudget = null
      }
    },
    'formData.purchaseNum'(val) {
      if (val) {
        if (this.formData.purchaseBudget === null || this.formData.purchaseBudget === '') {
          this.formData.purchaseTotalBudget = null
        } else {
          this.formData.purchaseTotalBudget = (val * this.formData.purchaseBudget).toFixed(2)
        }
      } else {
        this.formData.purchaseTotalBudget = null
      }
    }

  },
  created() {
    // 获取采购计划详情
    this.queryMainPurchase()
    // 字典请求初始化
    this.getGroupType()
  },
  methods: {
    filterDictText, // 字典检索
    // 数据字典查询
    getGroupType() {
      this.$store.dispatch('getGroupType', this.dictionaryType).then((data) => {
        // 遍历key存储相应的数据
        for (const key in data.bidTypeData) {
          /* 是否有数据*/
          if (data.bidTypeData[key]) {
            this[key] = data.bidTypeData[key]
          }
        }
      })
    },
    // 附件上传
    // principalFile(file, fileList) {
    //   this.principalList = fileList
    // },
    // 获取采购计划详情信息,无ID时自动生成编号
    queryMainPurchase() {
      restoreInitialValue(this.formData)
      const data = {
        planMainId: this.planmainid ? this.planmainid : '', // 采购计划ID
        planDetailId: this.dialogData.planAdd !== '00' ? this.dialogData.planDetailId : ''// 采购计划详情ID
      }
      queryMainPurchasePlanDetailInfo(data).then(res => {
        this.formData = {
          ...res.data,
          purchaseType: '00' // 计划编制'00'框架编制'01'
        }
        this.lineItemFileType.bizId = res.data.id
        this.formData.operation = '00'
        this.formData.purchaseTaxRate = this.formData.purchaseTaxRate ? this.formData.purchaseTaxRate + '' : ''
      }).catch(() => {})
    },
    // 拿到选择物资编码数据
    onRes(res) {
      this.purchaseData = res.data
      this.formData = Object.assign({
        ...this.formData
      }, {
        ... this.purchaseData
      })
      this.formData.purchaseTaxRate = this.formData.purchaseTaxRate ? this.formData.purchaseTaxRate + '' : ''
    },
    // 清空物资信息
    onPurchaseClear() {
      restoreInitialValue(this.purchaseData)
      this.formData = Object.assign({
        ...this.formData
      }, {
        ... this.purchaseData
      })
      // this.$refs.heandlerClose.clearItem()
    },
    // 拿到选择计划负责人
    onTakeRes(res) {
      if (res === 'clear') {
        this.formData.planManagerName = ''
        this.formData.planManagerId = ''
      } else {
        this.$set(this.formData, 'planManagerName', res.realName)
        this.$set(this.formData, 'planManagerId', res.userId)
      }
    },
    // 取消
    closeOf() {
      this.$emit('saveconsignContractInfo', true)
      this.dialogData.consignContractVisible = false
    },
    // 关闭
    closeOff() {
      this.$emit('saveconsignContractInfo', false)
    },
    // 录入确认参数
    getParams() {
      const index = this.formData.depotList.findIndex((item) => {
        return item.depotId === this.formData.depotId
      })
      const data = Object.assign({}, this.formData)
      // 税率
      // const taxRate = this.taxRateList.reduce((arr, pre) => {
      //   if (pre.typename === this.formData.purchaseTaxRate) {
      //     arr.push(pre.typecode)
      //   } else if (pre.typecode === this.formData.purchaseTaxRate + '') {
      //     arr.push(pre.typecode)
      //   }
      //   return arr
      // }, []).join(',')
      data.depotSite = index !== -1 ? this.formData.depotList[index].depotSite : ''
      data.planMainId = this.planmainid
      data.purchasePlanNo = this.dialogData.purchasePlanNo
      // data.purchaseTaxRate = taxRate
      return data
    },
    // 确认
    okdialog(formName) {
      const data = this.getParams()
      this.conformLoading = true
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          this.conformLoading = true
          savePurchasePlanDetailInfo(data).then(res => {
            this.$emit('saveconsignContractInfo', true)
            this.dialogData.consignContractVisible = false
          })
        } else {
          this.conformLoading = false
          this.$message.warning('请注意页面提示信息')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
