<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <a-form-item
        label="位号"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['no', {rules: [{required: true, message: '位号不能为空'}]}]" />
      </a-form-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>
      <a-form-item
        label="类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['type', {rules: [{required: true, message: '类型不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in positionTypeMap"
            :key="value"
            :label="label"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="上层位置"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-tree-select
          style="width: 100%"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="treeData"
          :treeNodeFilterProp="'title'"
          :showSearch="true"
          v-decorator="['parentId', {rules: [{required: false, message: '上层位置不能为空'}]}]"
          placeholder="请选择"
        >
        </a-tree-select>
      </a-form-item>
<!--      <a-form-item
        label="父子关联编码"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['code', {rules: [{required: true, message: '父子关联编码不能为空'}]}]" />
      </a-form-item>-->
      <a-form-item
        label="区域维修负责人"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['userId', {rules: [{required: true, message: '仓库负责人不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="({userId, realName}) in userList"
            :key="userId"
            :label="realName"
            :value="userId">{{ realName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="排序"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="1"
          v-decorator="['sort', {initialValue:1,rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <row-item>
        <a-form-item
          label="图片"
          :labelCol="BaseTool.Constant.labelCol"
          :wrapperCol="BaseTool.Constant.wrapperCol"
        >
          <upload-spare-file
            ref="imageUpload"
            @catchImage="catchImage"
          ></upload-spare-file>
        </a-form-item>
      </row-item>
      <a-form-item
        label="是否删除"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['delFlag', {initialValue:DictCache.VALUE.DELFLAG.NORMAL, rules: [{required: true, message: '是否删除不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in delFlagMap"
            :key="value"
            :label="label"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="备注"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['remark', {rules: [{required: false, message: '备注不能为空'}]}]" />
      </a-form-item>
    </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 { addSbPosition, updateSbPosition, getSbPositionTree } from '@/api/sb/position'
import { queryRepairUser } from '@/api/upms/user-dept'

export default {
  name: 'BaseSbPosition',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      img: '',
      positionTypeMap: {},
      delFlagMap: {},
      userList: {},
      treeData: []
    }
  },
  props: {
  },
  created () {
    this.delFlagMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.DELFLAG)
    this.positionTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBPOSITION_TYPE)
    const params = { roleType: 3, deptId: null, queryType: 1 }
    queryRepairUser(params).then(res => {
      this.userList = res.data
    })
  },
  methods: {
    base (record) {
      this.setTree(record)
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '复制'
      }
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'name',
          'type',
          'code',
          'sort',
          'delFlag',
          'parentId',
          'userId',
          'remark'
        ])))
      })
      this.img = record.img
      const fileList = []
      if (record.img) {
        fileList.push({
          uid: '-1',
          name: record.name,
          status: 'done',
          url: this.BaseTool.Constant.FILE_URL + record.img,
          originUrl: record.img
        })
      }
      this.$refs.imageUpload.base(1, fileList)
    },
    catchImage (fileList) {
      if (fileList.length !== 0) {
        this.img = fileList[0].url
      }
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.img = this.img
        if (this.BaseTool.String.isBlank(values.id)) {
          addSbPosition(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateSbPosition(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    /**
     * 设置位置树
     */
    setTree (record = {}) {
      getSbPositionTree({ id: record.id }).then(res => {
        console.log(res.data)
        this.treeData = res.data
      })
    }

  }
}
</script>
