<template>
  <el-dialog
    title="理化报告单"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    width="1300px"
    @close="close"
    @open="open"
  >
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="单据号">
            <el-input
              v-model="query.inspection_code"
              clearable
              size="mini"
              placeholder="请输入质检单号"
              style="width: 210px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="单据类型">
            <el-select
              v-model="query.inspection_type"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 210px;"
              @change="hand"
            >
              <el-option
                v-for="item in dict.QC_INV_TYPE_LHD"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="日期">
            <date-range-picker v-model="query.createTime" class="date-item" style="width: 180px" />
          </el-form-item>

          <el-form-item label="物料">
            <el-input
              v-model="query.material_code"
              clearable
              size="mini"
              placeholder="请输入物料编码或名称"
              style="width: 210px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="批号">
            <el-input
              v-model="query.pcsn"
              clearable
              size="mini"
              placeholder="请输入物料批号"
              style="width: 210px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="单据状态">
            <el-select
              v-model="query.bill_status"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 210px;"
              @change="hand"
            >
              <el-option
                v-for="item in statusList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <rrOperation />
          </el-form-item>
        </el-form>
      </div>

      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="false" prop="inspection_id" label="理化质检单标识" />
        <el-table-column prop="inspection_code" label="质检单号" width="135" />
        <el-table-column prop="inspection_type" label="单据类型" :formatter="format_inspection_type" />
        <el-table-column prop="bill_status" label="单据状态" :formatter="formatStatus" />
        <el-table-column v-if="false" prop="biz_date" label="业务日期" />
        <el-table-column v-if="false" prop="material_id" label="物料标识" />
        <el-table-column prop="material_code" label="物料编码" min-width="120" show-overflow-tooltip />
        <el-table-column prop="material_name" label="物料名称" min-width="120" show-overflow-tooltip />
        <el-table-column prop="pcsn" label="批号" min-width="100" show-overflow-tooltip />
        <el-table-column prop="is_effective" label="是否有效" :formatter="format_is_active" />
        <el-table-column prop="result" label="结果" :formatter="format_result" />
        <el-table-column prop="remark" show-overflow-tooltip label="备注" />
        <el-table-column prop="input_optname" label="创建人" />
        <el-table-column prop="input_time" label="创建时间" width="135" />
        <el-table-column prop="check_optname" label="检验人" />
        <el-table-column prop="check_time" label="检验时间" width="135" />
        <el-table-column prop="confirm_optname" label="结论判定人" />
        <el-table-column prop="confirm_time" label="结论判定时间" width="135" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import crudPhysicalMst from '@/api/wms/ql/physicalMst'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudInspectionsheetmst from '@/api/wms/ql/inspectionsheetmst'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = {
  inspection_id: null,
  inspection_code: null,
  inspection_type: null,
  inspection_id_gu: null,
  material_id: null,
  pcsn: null
}
export default {
  name: 'PhysicalMst',
  dicts: ['QC_INV_TYPE_LHD', 'IS_OR_NOT', 'QC_RESULT_LHZJD'],
  components: { pagination, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  cruds() {
    return CRUD({
      title: '理化报告单',
      url: 'api/physicalMst',
      idField: 'sample_code',
      sort: 'sample_code,desc',
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true
      },
      crudMethod: { ...crudPhysicalMst }
    })
  },
  data() {
    return {
      materShow: false,
      visiable1: false,
      inspection_id: null,
      statusList: [],
      permission: {},
      dialogVisible: false
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  created() {
    crudInspectionsheetmst.getStatus().then(res => {
      this.statusList = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    format_inspection_type(row, column) {
      return this.dict.label.QC_INV_TYPE_LHD[row.inspection_type]
    },
    formatStatus(row, column) {
      for (const item of this.statusList) {
        if (item.value === row.bill_status) {
          return item.label
        }
      }
    },
    format_is_active(row, column) {
      return this.dict.label.IS_OR_NOT[row.is_effective]
    },
    format_result(row, column) {
      return this.dict.label.QC_RESULT_LHZJD[row.result]
    },
    open() {
      this.crud.toQuery()
    },
    sure() {
      const _selectData = this.$refs.table.selection
      if (_selectData.length !== 1) {
        return this.crud.notify('只能选择一条记录', CRUD.NOTIFICATION_TYPE.ERROR)
      }
      if (_selectData[0].inspection_type !== '20') {
        return this.crud.notify('请选择钴粉类型', CRUD.NOTIFICATION_TYPE.ERROR)
      }
      this.$emit('update:dialogShow', false)
      // 将当前表单中的数据传回父页面
      this.rows = this.$refs.table.selection
      this.$emit('RelevancyChanged', this.rows)
    },
    close() {
      this.$emit('update:dialogShow', false)
      this.$refs['form'].resetFields()
      this.$refs['table'].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
