<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 || crud.status.view > 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"
            :disabled="crud.status.view > 0"
            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"
      :inline="true"
      :model="form"
      :rules="rules"
      style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;"
      class="demo-form-inline"
      size="mini"
      label-width="auto"
      label-position="right"
      label-suffix=":"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="工艺路线号" prop="processroute_code">
            <el-input
              v-model="form.processroute_code"
              :disabled="crud.status.view > 0"
              size="mini"
              style="width: 210px"
              class="filter-item"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工艺路线名称" prop="processroute_name">
            <el-input
              v-model="form.processroute_name"
              :disabled="crud.status.view > 0"
              size="mini"
              style="width: 210px"
              class="filter-item"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="生产车间" prop="processroute_name">
            <el-select
              v-model="form.product_area"
              clearable
              filterable
              size="mini"
              style="width: 210px;"
              placeholder="区域类型"
              class="filter-item"
              @change="crud.toQuery()"
            >
              <el-option
                v-for="item in dict.product_area"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <label slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</label>
            <el-input v-model="form.remark" style="width: 380px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
    <div class="crud-opts2" style="margin-top: 30px">
      <span class="role-span">工艺路线明细</span>
      <span v-if="crud.status.cu > 0 || crud.status.view > 0" class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="crud.status.view > 0"
          @click="insertLine"
        >
          新增一行
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="danger"
          icon="el-icon-close"
          size="mini"
          :disabled="crud.status.view > 0"
          @click="delLine"
        >
          删除一行
        </el-button>
      </span>
    </div>

    <!--表格渲染-->
    <el-table
      ref="table"
      :data="form.tableData"
      style="width: 100%;"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="index" label="序号" v-model="form.tableData.workprocedure_no" width="50" align="center" />
      <el-table-column prop="workprocedure_name" label="工序名称" align="center">
        <template slot-scope="scope">
          <el-select style="width: 200px" size="mini" clearable class="edit-input" v-model="form.tableData[scope.$index].workprocedure_id" :disabled="crud.status.view > 0"  @change="getName(scope.row)" placeholder="请选择">
            <el-option
              v-for="item in workList"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="workprocedure_code" label="工序编码" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-input style="width: 200px" v-model="form.tableData[scope.$index].workprocedure_code" size="mini" class="edit-input" disabled />
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
import crudProcessroute from '@/api/wms/pdm/processroute'
import CRUD, { crud, form } from '@crud/crud'

const defaultForm = {
  processroute_id: null,
  processroute_code: null,
  processroute_name: null,
  processroute_status: null,
  product_area: null,
  remark: null,
  tableData: []
}

export default {
  name: 'AddDialog',
  mixins: [form(defaultForm), crud()],
  dicts: ['ST_INV_TYPE_RC', 'product_area'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      workList: [],
      rules: {
        processroute_code: [
          { required: true, message: '工艺路线编码不能为空', trigger: 'blur' }
        ],
        processroute_name: [
          { required: true, message: '工艺路线名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  created() {
    crudProcessroute.getWorkList().then(res => {
      this.workList = res
    })
  },
  methods: {
    [CRUD.HOOK.afterToEdit]() {
      crudProcessroute.getDtl({ 'processroute_id': this.form.processroute_id }).then(res => {
        this.form.tableData = res
      })
    },
    [CRUD.HOOK.beforeSubmit]() {
      const data = this.form.tableData
      if (data.length === 0) {
        this.crud.notify('请至少新增一条工艺路线', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      for (let i = 0; i < data.length; i++) {
        const item = data[i]
        if (!item.workprocedure_id) {
          this.crud.notify('工序号或者工序名称不能为空', CRUD.NOTIFICATION_TYPE.INFO)
          return false
        }
      }
    },
    [CRUD.HOOK.afterToView]() {
      crudProcessroute.getDtl({ 'processroute_id': this.form.processroute_id }).then(res => {
        this.form.tableData = res
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    close() {
      this.$emit('update:dialogShow', false)
    },
    insertLine() {
      this.form.tableData.push({ workprocedure_no: '', is_check: '1' })
    },
    delLine() {
      this.form.tableData.splice(-1)
    },
    getName(val) {
      const arr = this.workList
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].value === val.workprocedure_id) {
          val.workprocedure_code = arr[i].code
        }
      }
    }
  }
}
</script>

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

.crud-opts2 .crud-opts-right2 {
  margin-left: auto;
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 2px;
}
</style>
