<template>
  <a-modal
    :title="modalTitle"
    :width="850"
    :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>

      <row-list :col="1">
        <row-item>
          <a-form-item
            label="项目名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input

              v-decorator="['name', {rules: [{required: false, message: '项目名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="编码"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['no', {rules: [{required: false, message: '编码不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备型号id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['modelId', {rules: [{required: true, message: '设备型号id不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备型号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              style="width: 80%"
              v-decorator="['sbNameModel', {rules: [{required: true, message: '设备型号不能为空'}]}]"/>
            <a-button type="primary" style="width: 20%" @click="handleSbModelSelect">选择</a-button>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备型号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['sbModel', {rules: [{required: false, message: '设备型号不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['sbName', {rules: [{required: false, message: '设备名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['sbId']"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备编号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              style="width: 80%"
              v-decorator="['sbNo']"/>
            <a-button type="primary" style="width: 20%" @click="handleSbSelect">选择</a-button>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="部件id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['partId', {rules: [{required: false, message: '部件id不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="部件编码"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="showPart"
          >
            <a-input
              disabled
              style="width: 80%"
              v-decorator="['partNo', {rules: [{required: false, message: '部件id不能为空'}]}]"/>
            <a-button type="primary" style="width: 20%" @click="handlePartSelect">选择</a-button>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="部件名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="showPart"
          >
            <a-input
              disabled
              v-decorator="['partName', {rules: [{required: false, message: '部件id不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="检查类型"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              disabled
              v-decorator="['type', {initialValue:checkType,rules: [{required: true, message: '检查类型不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in typeMap"
                :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="['enable', {initialValue:1,rules: [{required: true, message: '是否启用不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in enableMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <a-tabs type="card" default-active-key="1">
      <a-tab-pane key="1" tab="检查标准">
        <div class="table-operator">
          <a-button size="small" type="primary" @click="handleStandardSelect">
            <a-icon type="plus"/>
            添加
          </a-button>
          <a-button class="margin-left8" size="small" type="danger" @click="handleDel">
            <a-icon type="delete"/>
            删除
          </a-button>
        </div>
        <a-table
          bordered
          :data-source="data"
          :columns="columns"
          tableLayout="auto"
          rowKey="id"
          :row-selection="rowSelection">
          <span slot="action" slot-scope="record1">
            <template>
              <a @click="handleView(record1)">查看</a>
            </template>
          </span>
        </a-table>
      </a-tab-pane>
    </a-tabs>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <sb-info-select-modal ref="sbInfoSelectModal" @selected="handleSbSelectd"/>
    <sb-model-select-modal ref="sbModelSelectModal" @selected="handleSbModelSelectd"/>
    <part-info-select-modal ref="partInfoSelectModal" @selected="handlePartSelected"/>
    <check-standard-select-modal :type="'checkbox'" :check-type="checkType" ref="checkStandardSelectModal" @selected="handleStandardSelected"/>
    <detail ref="detailModal"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addCheckProject } from '@/api/check/checkproject'
import PartInfoSelectModal from '@/views/part/info/modules/PartInfoSelectModal'
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'
import SbModelSelectModal from '@/views/sb/model/modules/SbModelSelectModal'
import CheckStandardSelectModal from '@/views/check/checkstandard/modules/CheckStandardSelectModal'
import Detail from '../../checkstandard/modules/Detail'
import { fetchCheckStandard } from '@/api/check/checkstandard'

export default {
  name: 'BaseCheckProject',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      showPart: false,
      // 下拉框map,
      enableMap: {},
      typeMap: {},
      periodTypeMap: {},
      actionTypeMap: {},
      rowSelection: {
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows
        }
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return index + 1
          }
        },
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '周期',
          dataIndex: 'periodType',
          customRender: (text, record, index) => {
            return record.period + this.BaseTool.Table.getMapText(this.periodTypeMap, text)
          }
        },
        //{
         // title: '要求',
         // dataIndex: 'requirement'
        //},
        {
          title: '操作',
          key: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      data: [],
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys
        }
      }
    }
  },
  components: {
    SbModelSelectModal,
    SbInfoSelectModal,
    PartInfoSelectModal,
    CheckStandardSelectModal,
    Detail
  },
  props: {
    /**
     * 检查类型： 1-点检 2-巡检
     */
    checkType: {
      type: Number,
      default: 1
    }
  },
  created () {
    // 下拉框map
    this.enableMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_STANDARD_TYPE)
    this.periodTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_PERIOD_TYPE)
    this.actionTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_ACTION_TYPE)

  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      this.showPart = false
      this.modalTitle = '复制'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'name',
          'modelId',
          'sbModel',
          'sbNameModel',
          'sbId',
          'sbNo',
          'sbName',
          'sbType',
          'partId',
          'type',
          'partNo',
          'partName'
        ])))
      })
      this.data = record.standardList !== null ? record.standardList : []
    },
    handleView (record) {
      fetchCheckStandard({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        modal.base(res.data)
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        if (this.data.length === 0) {
          this.$message.error('请添加检查标准')
          this.confirmLoading = false
          return
        }
        values.standardList = this.data
        // 日期处理
        addCheckProject(values)
          .then(() => {
            this.handleCancel(values)
            this.$message.info("复制成功")
          }).catch(() => {
            this.confirmLoading = false
            this.$message.info("出现错误")
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    handleSbModelSelect () {
      this.$refs.sbModelSelectModal.base()
    },
    handleSbModelSelectd (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'modelId': key,
          'sbName': row.name,
          'sbNameModel': row.nameModel,
          'sbModel': row.model
        }))
        this.showPart = true
      })
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base()
    },
    handleSbSelectd (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'sbId': key,
          'sbNo': row.no
        }))
        this.showPart = true
      })
    },
    handlePartSelect () {
      const sbId = this.form.getFieldValue('sbId')
      this.$refs.partInfoSelectModal.base({ sbId })
    },
    handlePartSelected (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'partId': key,
          'partName': row.name,
          'partNo': row.no
        }))
      })
    },
    handleStandardSelect () {
      this.$refs.checkStandardSelectModal.base({}, { enable: 1 })
    },
    handleStandardSelected (record, keys, rows) {
      const { data } = this
      const len = data.length
      for (let i = 0; i < rows.length; i++) {
        const row = rows[i]
        let flag = false
        for (let j = 0; j < len; j++) {
          if (row.id === data[j].id) {
            flag = true
          }
        }
        if (!flag) {
          data.push(row)
        }
      }
    },
    handleDel () {
      const data = [...this.data]
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请至少选择一条数据')
        return
      }
      this.data = data.filter(item => !this.selectedRowKeys.includes(item.id))
    }

  }
}
</script>
