<!--
 * @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>
                <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="'334px'" :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: 100%">
                  <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="'334px'" :money-value="formData" name="purchaseBudget" />
              </td>
              <td> <el-form-item label="预算净总价(元)" prop="purchaseTotalBudget" /></td>
              <td>
                <!--金额组件-->
                <money :thousands="true" :disabled="true" :width="'336px'" :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="" /></td>
              <td colspan="3">
                <el-input
                  v-model="formData.purchaseDepartName"
                  placeholder="请选择"
                  style="width: calc(100% - 200px)"
                  disabled
                />
                <el-button type="primary" class="ml10 choose" @click="organSelect">
                  选择采购单位
                </el-button>
                <el-button type="primary" plain class="ml10 choose" @click="organSelect('clear')">
                  清 空
                </el-button>
              </td>
            </tr>
            <tr>
              <!-- 计划负责人 暂时隐藏 -->
              <!-- <td>
                <el-form-item label="计划负责人" prop="planManagerName" />
              </td>
              <td>
                <gx-input
                  v-model="formData.planManagerName"
                  show-word-limit
                  gx-maxlength="varchar50"
                  class="show-word-limit-padding"
                  style="width: calc(100% - 156px );"
                  :disabled="true"
                />
                <el-button type="primary" @click="dialogTakeData.dialogVisibleTake=true">
                  选择
                </el-button>
                <el-button plain @click="onTakeRes('clear')">
                  清空
                </el-button>
              </td> -->
              <td> <el-form-item ref="deliveryTime" label="交货日期" prop="deliveryTime" /></td>
              <td>
                <span>以实际项目情况为准</span>
              </td>
              <td>
                <el-form-item :label-position="labelposition" :inline-message="true" prop="depotId" label="库存地点" />
              </td>
              <td>
                <span>以实际项目情况为准</span>
              </td>
            </tr>
            <tr>
              <td><el-form-item label="交货地点" /></td>
              <td colspan="3">
                <span>以实际项目情况为准</span>
              </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">
                <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="closeOff">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 选择物料编码 -->
    <selGoogsDialog ref="heandlerClose" :dialog-son-data="dialogSonData" @onRes="onRes" />
    <!-- 选择计划负责人 -->
    <!-- <purPlanEntryTakeDialog :dialog-take-data="dialogTakeData" @onTakeRes="onTakeRes" /> -->
    <!-- 采购单位弹框 -->
    <organTree-dialog ref="organTree" @call-back="organTree" />
  </div>
</template>
<script>
/* api */
import { savePurchasePlanDetailInfo, queryMainPurchasePlanDetailInfo } from '@/api/procurement/purchasplan'// 采购平台录入行项目信息
/** 公共方法 */
import { restoreInitialValue } from '@/assets/js/utils' // 清空
import { filterDictText } from '@/assets/js/utils'// 常用方法
import { procurementMixins } from '@/views/procurement/mixins/procurementMixins'
import { checkNum, characters1_50, characters1_auto, checkMyriadMoney } from '@/assets/js/validate'// 校验输入内容
/** 公共组件 */
import uploadFile from '@/views/procurement/procurementPlan/components/uploadFile' // 附件上传
import organTreeDialog from '@/views/common/user/organizationTree/organTree-dialog' // 采购单位选择

import money from '@comp/common/money/money' // 金币组件
/* 弹框 */
import selGoogsDialog from '@/views/procurement/procurementPlan/components/selGoogs-dialog' // 选择物料编码
// import purPlanEntryTakeDialog from './purPlanEntryTake-dialog' // 选择计划负责人
export default {
  name: 'AddLineItemDialog',
  components: {
    uploadFile, // 附件上传
    money, // 金额组件
    selGoogsDialog, // 物料编码
    organTreeDialog // 采购单位
    // purPlanEntryTakeDialog // 计划负责人
  },
  mixins: [procurementMixins],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return {}
      }
    },
    // 计划id
    planmainid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      /* -----------数据字典start-------------*/
      purchaseUnitList: [], // 计量单位
      taxRateList: [], // 税率
      collectionTypeList: [], // 是否集采
      dictionaryType: ['purchaseUnit', 'taxRate', 'collectionType'],
      /* -----------数据字典end-------------*/
      value: '',
      dialogSonData: {
        dialogVisibleGoogs: false // 选择物资编码
      },
      dialogTakeData: {
        dialogVisibleTake: false // 选择计划负责人
      },
      conformLoading: false,
      labelposition: 'right', // label位置
      formData: {
        // deliveryAddCompany: '已实际项目为准', // 交货地点
        // deliveryTime: '已实际项目为准', // 交货日期
        // depotId: '已实际项目为准', // 库存地点
        deliveryAreaCode: '',
        operation: '00', // 保存
        purchaseUnit: '', // 计量单位
        purchaseDepartName: '', // 采购单位
        purchaseDepartId: '' // 代理单位ID
      },
      purchaseData: {}, // 物资编码信息
      // 校验规则
      rules: {
        // purchaseDepartName: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 采购单位
        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' }], // 税率
        // 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' }] // 预算净单价
      }
    }
  },
  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.getGroupType()// 字典请求初始化
    this.queryMainPurchase() // 获取采购计划详情
  },
  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]
          }
        }
      })
    },
    // 获取采购计划详情信息,无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: '01' // 计划编制'00'框架编制'01'
        }
        this.lineItemFileType.bizId = res.data.id
        this.formData.operation = '00' // 保存 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
      })
    },
    // 拿到选择计划负责人
    // 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)
    //   }
    // },
    /* ======================采购单位Start============================*/
    // 选择采购单位 type 有值为清空 无值为新增
    organSelect(type) {
      if (type === 'clear') {
        this.formData.purchaseDepartName = ''
      } else {
        this.$refs.organTree.open({
          title: '选择采购单位', // 弹框标题
          dialog: true, // 弹窗状态
          selectCodes: this.formData.purchaseDepartId ? this.formData.purchaseDepartId.split(',') : this.formData.purchaseDepartId // 当前选中的供货范围Code
        })
      }
    },
    // 选择/清空采购单位
    organTree(data) {
      // 清空
      if (data === 'clear') {
        this.formData.purchaseDepartName = '' // 采购单位名称
        this.formData.purchaseDepartId = '' // 采购单位id
      } else {
        // 选择
        this.$set(this.formData, 'purchaseDepartName', data.labels)
        this.$set(this.formData, 'purchaseDepartId', data.keys)
      }
    },
    /* ======================采购单位End============================*/

    // // 取消
    // closeOf() {
    //   this.$emit('saveconsignContractInfo')
    //   this.dialogData.consignContractVisible = false
    // },
    // 关闭
    closeOff() {
      // this.$emit('saveconsignContractInfo')
      this.dialogData.consignContractVisible = false
    },
    // 录入确认参数
    getParams() {
      const data = Object.assign({}, this.formData)
      data.planMainId = this.planmainid // 计划id
      data.planType = '01' // 框架 01
      data.purchasePlanNo = this.dialogData.purchasePlanNo // 计划编号
      data.purchaseDepartmentId = this.formData.purchaseDepartId // 采购部门id
      data.purchaseDepartmentName = this.formData.purchaseDepartName // 采购部门名称
      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', res.data)
            this.dialogData.consignContractVisible = false
          }).catch(() => {
            this.conformLoading = false
          })
        } else {
          this.conformLoading = false
          this.$message.warning('请注意页面提示信息')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
