<script xmlns="http://www.w3.org/1999/html">
import {editDrawing, removeDrawing, searchDrawingDetailList, searchDrawingList} from "../../../api/code/codeDrawing";
import dayjs from "dayjs";
import {queryCodeErpPartNo} from "../../../api/code/codeParameterDef";

export default {
  name: "CodeList",
  data(){
    return{
      drawingList:[],
      searchModel:{
        site:this.$store.state.user.site,
        drawingNo:undefined,
        creator:undefined,
        drawingDesc:undefined,
        remark:undefined,
        erpPartNo:undefined,
        erpPartDesc:undefined,
        startDate:"",
        endDate:"",
      },
      drawing:{

      },
      drawingDetailList:[],
      detailLoading:false,
      detailDialogVisible:false,
      saveDialogVisible:false,
      columns: [
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1DrawingNo',
          tableId: '9009003Table1',
          tableName: '编码信息',
          columnProp: 'drawingNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 140
        },
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1DrawingDesc',
          tableId: '9009003Table1',
          tableName: '编码描述',
          columnProp: 'drawingDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 600
        },
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1ErpPartNo',
          tableId: '9009003Table1',
          tableName: '编码描述',
          columnProp: 'erpPartNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: 'IFS Part No',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1ErpPartDesc',
          tableId: '9009003Table1',
          tableName: '编码描述',
          columnProp: 'erpPartDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: 'IFS Part Description',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 180
        },
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1Creator',
          tableId: '9009003Table1',
          tableName: '编码描述',
          columnProp: 'creator',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '创建人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1CreateDate',
          tableId: '9009003Table1',
          tableName: '编码描述',
          columnProp: 'createDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 9009003,
          serialNumber: '9009003Table1Remark',
          tableId: '9009003Table1',
          tableName: '编码描述',
          columnProp: 'remark',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '备注',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
      ],
      exportData:[],
      exportName: '编码列表'+this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: ["编码列表"],
      exportFooter: [],
    }
  },
  created() {
    this.searchModel.startDate = dayjs().hour(0).minute(0).second(0).format("YYYY-MM-DD HH:mm:ss");
    this.searchDrawingList();
  },
  methods:{
    fields() {
      let json = "{"
      this.columns.forEach((item, index) => {
        if (index === this.columns.length - 1) {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
        } else {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
        }
      })
      json += "}"
      return eval("(" + json + ")")
    },
    createExportData() {
      return this.drawingList;
    },
    searchDrawingList(){
      searchDrawingList(this.searchModel).then(({data})=>{
        if (data && data.code === 0){
          this.drawingList = data.rows;
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
      })
    },
    handleDelete(row){
      this.$confirm(`该操作将删除编码${row.drawingNo}, 是否继续?`).then(() => {
        removeDrawing(row).then(({data})=>{
          if (data && data.code === 0){
            this.searchDrawingList();
            this.$message.success(data.msg);
          }else {
            this.$message.warning(data.msg);
          }
        }).catch((error)=>{
          this.$message.error(error);
        });
      })
    },
    handleEdit(row){
      this.drawing = {...row}
      this.saveDialogVisible = true;
    },
    editDrawing(){
      if (this.drawing.erpPartNo && !this.drawing.erpPartDesc){
        this.$message.warning("IFS Part No 不存在");
        return;
      }
      editDrawing(this.drawing).then(({data})=>{
        if (data && data.code === 0){
          this.searchDrawingList();
          this.$message.success(data.msg);
          this.saveDialogVisible = false;
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
      });
    },
    handleBlurErpPartNo(){
      let params = {
        erpPartNo:this.drawing.erpPartNo,
      }
      queryCodeErpPartNo(params).then(({data})=>{
        if (data && data.code === 0){
          if (data.row){
            this.drawing.erpPartDesc = data.row.partDesc
          }else {
            this.drawing.erpPartDesc = ''
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    handleDetail(row){
      this.detailLoading = true;
      this.drawingDetailList = [];
      this.detailDialogVisible = true;
      searchDrawingDetailList(row).then(({data})=>{
        if (data && data.code === 0){
          this.drawingDetailList = data.rows;
        }else {
          this.$message.warning(data.msg);
        }
        this.detailLoading = false;
      }).catch((error)=>{
        this.$message.error(error);
        this.detailLoading = false;
      })
    }
  }
}
</script>

<template>
<div>
  <el-form label-position="top" :model="searchModel">
    <el-row :gutter="10">
      <el-col :span="3">
        <el-form-item label="编码:">
          <el-input v-model="searchModel.drawingNo"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="编码描述:">
          <el-input v-model="searchModel.drawingDesc" ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="备注:">
          <el-input v-model="searchModel.remark" ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="创建人:">
          <el-input v-model="searchModel.creator" ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="3">
        <el-form-item label="IFS Part No:">
          <el-input v-model="searchModel.erpPartNo" ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="IFS Part Description:">
          <el-input v-model="searchModel.erpPartDesc" ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="开始日期:">
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="searchModel.startDate"
            style="width: 100%"
            type="date"
            placeholder="开始日期">

          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="结束日期:">
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="searchModel.endDate" style="width: 100%" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label=" ">
          <el-button type="primary" @click="searchDrawingList">搜索</el-button>
          <download-excel
            :fields="fields()"
            :data="exportData"
            type="xls"
            :name="exportName"
            :header="exportHeader"
            :footer="exportFooter"
            :fetch="createExportData"
            worksheet="导出信息"
            class="el-button el-button--primary el-button--medium">
            {{ '导出' }}
          </download-excel>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <el-table :data="drawingList" height="750" border style="width: 100%">
    <el-table-column
      v-for="(item,index) in columns" :key="index"
      :sortable="item.columnSortable"
      :prop="item.columnProp"
      :header-align="item.headerAlign"
      :show-overflow-tooltip="item.showOverflowTooltip"
      :align="item.align"
      :fixed="item.fixed===''?false:item.fixed"
      :min-width="item.columnWidth"
      :label="item.columnLabel">
      <template slot-scope="scope">
        <template>
          <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </template>
    </el-table-column>
    <el-table-column header-align="center" width="140" align="center" fixed="right" label="操作">
      <template slot-scope="{row}">
        <el-link style="cursor:pointer;" type="text" @click="handleEdit(row)">编辑</el-link>
        <el-link style="cursor:pointer;" type="text" @click="handleDelete(row)">删除</el-link>
        <el-link style="cursor:pointer;" type="text" @click="handleDetail(row)">详情</el-link>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog title="编辑编码" :close-on-click-modal="false" :visible.sync="saveDialogVisible" width="1000px">
<!--    <fieldset-->
<!--      style="margin-top: 2px;border-color: rgb(255,255,255);">-->
<!--      <legend>更改前信息</legend>-->
      <el-form label-position="top" :model="drawing" ref="drawingForm">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="编码:">
              <el-input readonly v-model="drawing.drawingNo" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="IFS Part No:">
              <el-input v-model="drawing.erpPartNo" @blur="handleBlurErpPartNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="IFS Part Description:">
              <el-input v-model="drawing.erpPartDesc" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编码描述:">
              <el-input v-model="drawing.drawingDesc" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注:">
              <el-input v-model="drawing.remark" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
<!--    </fieldset>-->
<!--    <fieldset-->
<!--      style="margin-top: 2px;border-color: rgb(255,255,255);">-->
<!--      <legend>更改后信息</legend>-->
<!--      <el-form label-position="top" :model="drawing" ref="drawingForm">-->
<!--        <el-form-item label="编码:">-->
<!--          <el-input v-model="drawing.drawingNo" style="width: 200px"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="编码描述:">-->
<!--          <el-input readonly v-model="drawing.drawingDesc"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备注:">-->
<!--          <el-input readonly v-model="drawing.remark"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--    </fieldset>-->
    <el-footer style="height:30px;margin-top: 20px;text-align:center">
      <el-button type="primary" @click="editDrawing">保存</el-button>
      <el-button type="primary" @click="saveDialogVisible = false">关闭</el-button>
    </el-footer>
  </el-dialog>

  <el-dialog title="编码详情" :close-on-click-modal="false" v-drag :visible.sync="detailDialogVisible" :loading="detailLoading">
    <el-table :data="drawingDetailList" height="400" border style="width: 100%">
      <el-table-column prop="itemNo" header-align="center" width="140" align="right" label="序号"></el-table-column>
      <el-table-column prop="itemDesc" header-align="center" width="140" align="left" label="描述"></el-table-column>
      <el-table-column prop="itemName" header-align="center" width="140" align="left" label="元素名称"></el-table-column>
      <el-table-column prop="itemType" show-overflow-tooltip header-align="center" align="left" label="类型"></el-table-column>
      <el-table-column prop="itemLabel" show-overflow-tooltip header-align="center" align="left" label="选中项描述"></el-table-column>
      <el-table-column prop="itemValue" show-overflow-tooltip header-align="center" align="left" label="选中项值"></el-table-column>
    </el-table>
  </el-dialog>
</div>
</template>

<style scoped>
</style>
