<template>
  <el-dialog
    title="新增"
    append-to-body
    :visible.sync="dialogVisible"
    fullscreen
    :before-close="handleClose"
    destroy-on-close
    @close="close"
    @open="open"
  >
    <el-row :gutter="20">
      <el-col :span="20" style="border: 1px solid white">
        <span />
      </el-col>
      <el-col :span="4">
        <span>
          <el-button icon="el-icon-check" size="mini" type="primary" @click="submit">保存</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="close">关闭</el-button>
        </span>
      </el-col>
    </el-row>

    <el-form ref="form1" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form1" size="mini" label-width="120px" label-suffix=":">
      <el-row>
        <el-col :span="6">
          <el-form-item label="计划编码" prop="maint_plan_code">
            <el-input v-model="form1.maint_plan_code" style="width: 200px;" disabled placeholder="系统自动生成" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="计划开始日期" prop="plan_start_date">
            <el-date-picker v-model="form1.plan_start_date" type="date" placeholder="选择日期" style="width: 200px" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="计划周期" prop="maintenancecycle">
            <el-select
              v-model="form1.maintenancecycle"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 200px"
            >
              <el-option
                v-for="item in dict.EM_DEVICE_BY_CYCLE"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划名称" prop="maint_plan_name">
            <el-input v-model="form1.maint_plan_name" style="width: 200px;" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
          <el-form-item label="设备" prop="devicerecord_id">
            <el-input v-model="form1.devicerecord_id" :disabled="true" style="width: 200px;">
              <el-button slot="append" icon="el-icon-plus" @click="putDevice" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form1.remark" style="width: 580px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="crud-opts2">
      <span class="role-span">项目明细</span>
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-check"
          size="mini"
          @click="putItem"
        >
          添加项目
        </el-button>
      </span>
    </div>

    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="table2"
        :data="form1.tableData"
        style="width: 100%;"
        max-height="500"
        border
        :highlight-current-row="true"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      >
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column show-overflow-tooltip prop="maint_item_code" label="保养项目编号" align="center" />
        <el-table-column show-overflow-tooltip prop="maint_item_name" label="保养项目名称" align="center" />
        <el-table-column show-overflow-tooltip prop="item_level" label="保养等级" align="center" :formatter="formatLevel" />
        <el-table-column show-overflow-tooltip prop="contents" label="保养内容" align="center" />
        <el-table-column show-overflow-tooltip prop="requirement" label="要求" align="center" />
        <el-table-column prop="dtl_remark" label="备注" align="center">
          <template slot-scope="scope">
            <el-input v-show="!scope.row.edit" v-model="scope.row.dtl_remark" class="input-with-select"/>
          </template>
        </el-table-column>
        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="delRow(scope.$index, form1.tableData)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <DeviceDialog :dialog-show.sync="deviceDialog" :is-single="true" @tableChanged2="tableChanged2"/>
    <ItemDialog :dialog-show.sync="itemDialog" :is-single="false" @tableChanged1="tableChanged1"/>

  </el-dialog>
</template>

<script>
import crudDevicemaintenanceplanmst from '@/api/wms/sb/devicemaintenanceplanmst'
import DeviceDialog from '@/views/wms/sb/upkeep/devicemaintenanceplan/DeviceDialog'
import ItemDialog from '@/views/wms/sb/upkeep/devicemaintenanceplan/ItemDialog'
import CRUD, { crud } from '@crud/crud'

export default {
  name: 'CopyAddDialog',
  dicts: ['EM_DEVICE_BY_CYCLE'],
  components: { ItemDialog, DeviceDialog },
  mixins: [crud()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  data() {
    return {
      deviceDialog: false,
      itemDialog: false,
      dialogVisible: false,
      form1: {
        tableData: []
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    open() {
      this.form1 = this.openParam.form
      this.form1.tableData = this.openParam.tabData
      this.form1.devicerecord_id = null
      this.form1.maint_plan_code = null
      this.form1.maint_plan_name = null
    },
    close() {
      this.$emit('update:dialogShow', false)
      this.crud.toQuery()
    },
    putDevice() {
      this.deviceDialog = true
    },
    putItem() {
      this.itemDialog = true
    },
    formatLevel(row, cloum) {
      if (row.item_level === '01') {
        return '日常'
      } else if (row.item_level === '02') {
        return '一级'
      } else if (row.item_level === '03') {
        return '二级'
      }
    },
    tableChanged1(row) {
      const arr = row
      const data = this.form1.tableData
      arr.forEach((item) => {
        if (this.form1.tableData.length !== 0) {
          this.fla = false
          for (let i = 0; i < data.length; i++) {
            if (data[i].maint_item_id === item.maint_item_id) {
              this.fla = true
            }
          }
          if (this.fla === false) {
            this.form1.tableData.push(item)
          }
        } else {
          this.form1.tableData.push(item)
        }
      })
    },
    tableChanged2(row) {
      this.form1.devicerecord_id = row.devicerecord_id
    },
    delRow(index, rows) {
      rows.splice(index, 1)
    },
    submit() {
      const data = this.form1
      if (data.maint_plan_name === null) {
        return this.crud.notify('计划名称不能为空', CRUD.NOTIFICATION_TYPE.INFO)
      }
      if (data.devicerecord_id === null) {
        return this.crud.notify('设备不能为空', CRUD.NOTIFICATION_TYPE.INFO)
      }
      if (data.maintenancecycle === null) {
        return this.crud.notify('保养周期不能为空', CRUD.NOTIFICATION_TYPE.INFO)
      }
      if (data.plan_start_date === null) {
        return this.crud.notify('计划开始日期不能为空', CRUD.NOTIFICATION_TYPE.INFO)
      }
      if (data.tableData.length === 0) {
        return this.crud.notify('明细不能为空', CRUD.NOTIFICATION_TYPE.INFO)
      }
      crudDevicemaintenanceplanmst.copyAdd(data).then(res => {
        this.dialogVisible = false
        this.crud.toQuery()
        this.crud.notify('新增成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    }
  }
}
</script>

<style>
.crud-opts2 {
  padding: 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}

.crud-opts2 .el-dialog__title2 {
  line-height: 24px;
  font-size:20px;
  color:#303133;
}

.crud-opts2 .role-span {
  padding: 10px 0px 10px 0px;
}
.crud-opts2 .crud-opts-form {
  padding: 10px 0px 0px 20px;
}

.input-with-select  {
  background-color: #fff;
}

</style>
