<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <row-list :col="1" v-show="false">
        <row-item>
          <a-form-item>
            <a-input v-decorator="['id']" type="hidden"/>
          </a-form-item>
          <a-form-item>
            <a-input v-decorator="['parentId']" type="hidden"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="2">

        <row-item>
          <a-form-item
            label="父级"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              :disabled="true"
              v-decorator="['parentName']"/>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="部门"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-tree-select
              v-decorator="['deptId', {rules: [{required: true, message: '部门不能为空'}]}]"
              :treeNodeFilterProp="'title'"
              placeholder="请选择部门"
              :allow-clear="true"
              :show-search="true"
              :replaceFields="{children:'children', title:'title', key:'key', value: 'id' }"
              :tree-default-expand-all="true"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :treeData="deptTreeData"
            />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="章次"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['surah', {rules: [{required: true, message: '章次不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['unit', {rules: [{required: true, message: '单位不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in unitMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="位置单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['siteUnit', {rules: [{required: true, message: '位置单位不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in unitMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="单价"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              @blur="getTotalPrice"
              v-decorator="['price', {rules: [{required: true, message: '单价不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="总量"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              @blur="getTotalPrice"
              v-decorator="['aggregate', {rules: [{required: true, message: '总量不能为空'}]}]"/>
          </a-form-item>
        </row-item>

        <row-item>

          <a-form-item
            label="总价"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['totalPrice', {rules: [{required: true, message: '单价不能为空'}]}]"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addFillProjectQuantity, updateFillProjectQuantity } from '@/api/fill/project-quantity'
import { getDeptTree } from '@/api/upms/dept'

export default {
  name: 'BaseFillProjectQuantity',
  data () {
    return {
      dept: {},
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      unitMap: {},
      parentItem: {},
      deptTreeData: []
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FILL_PROJECT_QUANTITY_UNIT)
    getDeptTree().then(res => {
      // this.deptTreeData = res.data
      this.deptTreeData = this.BaseTool.TREE.treeFilter(res.data, (item) => {
        if (item.item.nature === this.DictCache.VALUE.SYS_DEPT_NATURE.BAN_ZU) {
          return { delete: true }
        }
        const flag = this.DictCache.VALUE.SYS_DEPT_NATURE.JITUAN === item.item.nature ||
        this.DictCache.VALUE.SYS_DEPT_NATURE.FEN_GONG_SI === item.item.nature ||
          this.DictCache.VALUE.SYS_DEPT_NATURE.DEFAULT === item.item.nature
        item.selectable = !flag
        item.disabled = flag
      })
    })
    /* // 查询所有项目部
    queryDept({ nature: this.DictCache.VALUE.SYS_DEPT_NATURE.XIANG_MU_BU }).then(res => {
      this.projectDeptList = res.data
    }) */
  },
  methods: {
    base (record, modelParams) {
      this.parentItem = {}
      const { form: { setFieldsValue } } = this
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record.id)) {
        // 判断是否允许修改
        this.modalTitle = '添加'
        const parentItem = modelParams.parentItem
        this.parentItem = parentItem
        if (parentItem) {
          this.$nextTick(() => {
            setFieldsValue({ parentId: parentItem.id, parentName: parentItem.name, deptId: parentItem.deptId })
          })
        }
        return
      }
      this.modalTitle = '编辑'
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'deptId',
          'deptName',
          'surah',
          'name',
          'unit',
          'aggregate',
          'parentId',
          'parentName',
          'siteUnit',
          'price',
          'totalPrice',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addFillProjectQuantity(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateFillProjectQuantity(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    getTotalPrice () {
      const { form: { getFieldValue, setFieldsValue } } = this
      const aggregate = getFieldValue('aggregate')
      const price = getFieldValue('price')
      if (this.BaseTool.Object.isNotBlank(aggregate) && this.BaseTool.Object.isNotBlank(price)) {
        setFieldsValue({ totalPrice: this.BaseTool.Number.mul(aggregate, price) })
      }
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
