<template>
  <el-dialog
    :title="crud.status.title"
    append-to-body
    fullscreen
    :before-close="crud.cancelCU"
    :visible.sync="crud.status.cu > 0 || crud.status.view > 0"
  >
    <el-row v-show="crud.status.cu > 0" :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" :loading="crud.cu === 2" type="primary" @click="crud.submitCU">保存</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="crud.cancelCU">关闭</el-button>
        </span>
      </el-col>
    </el-row>

    <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" :rules="rules" size="mini" label-width="120px" label-suffix=":">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据号" prop="repair_code">
            <el-input v-model="form.repair_code" style="width: 200px;" disabled placeholder="系统自动生成" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="计划维修日期" prop="plan_start_date">
            <el-date-picker v-model="form.plan_start_date" type="date" placeholder="选择日期" style="width: 200px" value-format="yyyy-MM-dd" :disabled="crud.status.view > 0" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="维修类型" prop="maintenancecycle">
            <el-select
              v-model="form.maintenancecycle"
              disabled
              size="mini"
              placeholder="请选择"
              class="filter-item"
            >
              <el-option
                v-for="item in dict.EM_DEVICE_WX_INVTYPE"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="设备" prop="devicerecord_id">
            <el-input v-model="form.device_code" :disabled="true" style="width: 200px;">
              <el-button slot="append" icon="el-icon-plus" :disabled="crud.status.view > 0" @click="putDevice" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="故障等级" prop="fault_level">
            <el-select
              v-model="form.fault_level"
              size="mini"
              placeholder="请选择"
              :disabled="crud.status.view > 0"
              class="filter-item"
            >
              <el-option
                v-for="item in dict.EM_FAULT_LEVEL"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="故障描述" prop="fault_desc">
            <el-input v-model="form.fault_desc" style="width: 580px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.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"
          :disabled="crud.status.view > 0"
          @click="putItem"
        >
          添加项目
        </el-button>
      </span>
    </div>

    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="table2"
        :data="form.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="repair_item_code" label="维修项目编号" align="center" />
        <el-table-column show-overflow-tooltip prop="repair_item_name" 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" :disabled="crud.status.view > 0" 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" :disabled="crud.status.view > 0" @click.native.prevent="delRow(scope.$index, form.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" :open-param="openParam" @tableChanged1="tableChanged1"/>
  </el-dialog>
</template>

<script>

const defaultForm = {
  device_code: null,
  material_type_id: null,
  repair_code: null,
  devicerecord_id: null,
  maintenancecycle: '02',
  fault_desc: null,
  fault_level: null,
  remark: null,
  plan_start_date: null,
  tableData: []
}

import crudDevicerepairmst from '@/api/wms/sb/devicerepairmst'
import DeviceDialog from '@/views/wms/sb/upkeep/devicemaintenanceplan/DeviceDialog'
import ItemDialog from '@/views/wms/sb/repair/devicerepairplan/ItemDialog'
import CRUD, { form, crud } from '@crud/crud'

export default {
  name: 'AddDialog',
  dicts: ['EM_DEVICE_WX_INVTYPE', 'EM_FAULT_LEVEL'],
  components: { ItemDialog, DeviceDialog },
  mixins: [form(defaultForm), crud()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      deviceDialog: false,
      itemDialog: false,
      rules: {
        plan_start_date: [
          { required: true, message: '计划维修日期不能为空', trigger: 'blur' }
        ],
        maintenancecycle: [
          { required: true, message: '维修类型不能为空', trigger: 'blur' }
        ],
        devicerecord_id: [
          { required: true, message: '设备不能为空', trigger: 'blur' }
        ],
        fault_level: [
          { required: true, message: '故障等级不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    [CRUD.HOOK.beforeSubmit]() {
      const arr = this.form.tableData
      if (arr.length === 0) {
        this.crud.notify('明细不能为空', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
    },
    [CRUD.HOOK.afterToEdit]() {
      const data = {
        'repair_id': this.form.repair_id
      }
      crudDevicerepairmst.getDtl(data).then(res => {
        this.form.tableData = res
      })
    },
    [CRUD.HOOK.afterToView]() {
      const data = {
        'repair_id': this.form.repair_id
      }
      crudDevicerepairmst.getDtl(data).then(res => {
        this.form.tableData = res
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    close() {
      this.$emit('update:dialogShow', false)
    },
    putDevice() {
      this.deviceDialog = true
    },
    putItem() {
      this.openParam = this.form
      this.itemDialog = true
    },
    tableChanged1(row) {
      const arr = row
      const data = this.form.tableData
      arr.forEach((item) => {
        if (this.form.tableData.length !== 0) {
          this.fla = false
          for (let i = 0; i < data.length; i++) {
            if (data[i].repair_item_id === item.repair_item_id) {
              this.fla = true
            }
          }
          if (this.fla === false) {
            this.form.tableData.push(item)
          }
        } else {
          this.form.tableData.push(item)
        }
      })
    },
    tableChanged2(row) {
      this.form.devicerecord_id = row.devicerecord_id
      this.form.material_type_id = row.material_type_id
      this.form.device_code = row.device_name + '-' + row.device_code
    },
    delRow(index, rows) {
      rows.splice(index, 1)
    }
  }
}
</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>
