<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-form-item :label="'参数卡编码'">
        <el-input v-model="searchData.projectId" style="width: 130px"></el-input>
      </el-form-item>
      <el-form-item >
        <span slot="label" style="" @click="getBaseList(1010,1)"><a herf="#">测试料号</a></span>
        <el-input v-model="searchData.projectType" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'状态'">
        <el-input v-model="searchData.projectOwnerName" style="width: 130px"></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-button @click="search()" type="primary" style="margin-left: 2px;margin-top:0px">查询</el-button>
        <el-button @click="addOrUpdateHandle()" type="primary" style="margin-left: 2px;margin-top: 0px">新增</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ '导出' }}
        </download-excel>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      :height="height"
      border
      v-loading="dataListLoading"
      style="width: 100%; ">
      <el-table-column
        v-for="(item,index) in columnList1" :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">
          <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>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        fixed="right"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="updateModel(scope.row)">修改</a>
          <a type="text" size="small" @click="deleteData(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 1000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog
      width="600px"
      :title="!dataForm.id ?  '添加' :'编辑'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :inline="true" label-position="top" label-width="100px" :rules="dataRole">
        <el-form-item :label="'项目料号'">
          <el-input v-model="dataForm.testPartNo" style="width: 130px" disabled></el-input>
        </el-form-item>

        <el-form-item prop="partTypeDesc" :rules="dataRole.partTypeDesc">
          <span slot="label" style="" @click="getBaseList(1013,1)"><a herf="#">物料类型</a></span>
          <el-input v-model="dataForm.partTypeDesc" style="width: 130px" readonly  @focus="getBaseList(1013,1)" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px"  :rules="dataRole">
        <el-form-item :label="'物料名称'" prop="partName" :rules="dataRole.partName">
          <el-input v-model="dataForm.partName" style="width: 562px" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px" >
        <el-form-item :label="'物料描述'">
          <el-input v-model="dataForm.partDesc" style="width: 562px" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px" >
        <el-form-item :label="'物料描述'">
          <el-input v-model="dataForm.partSpec" style="width: 562px" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px" >
        <el-form-item :label="'物料图号'">
          <el-input v-model="dataForm.materialNumber" style="width: 562px" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px" >
        <el-form-item :label="'备注'">
          <el-input v-model="dataForm.remark" style="width: 562px" ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveData()">{{'确定'}}</el-button>
        <el-button type="primary" @click="visible = false">{{'关闭'}}</el-button>
      </span>
    </el-dialog>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="search" v-drag></add-or-update>
    <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import {
    technicalSpecificationListSearch,

  } from "@/api/sampleManagement/technicalSpecificationList.js"
  import Chooselist from '@/views/modules/common/Chooselist'
  import AddOrUpdate from './technicalSpecificationList'
  export default {
    components: {
      Chooselist,
      AddOrUpdate,
    },
    data() {
      return {
        addOrUpdateVisible:false,
        height:200,
        dataList: [],
        searchData: {
          page: 1,
          limit: 100,
          site: this.$store.state.user.site,
          username: this.$store.state.user.name,
          projectId: '',
        },
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        visible:false,
        dataListLoading: false,
        currentRow:'',
        columnList1:[
          {
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1CodeNo',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "codeNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "参数卡编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1Status',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "statusDesc",
            headerAlign: "center",
            align: "left",
            columnLabel: "状态",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 50
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1FinalPartNo',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "finalPartNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "最终产品编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1TestPartNo',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "testPartNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "项目料号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1PartName',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "partDesc",
            headerAlign: "center",
            align: "left",
            columnLabel: "物料名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1RevNo',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "revNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "版本号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 50
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1ECNFlag',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "ecnFlag",
            headerAlign: "center",
            align: "left",
            columnLabel: "ECN变更",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 50
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1EcnNo',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "ecnNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "ECN单据号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1ProofingNo',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "proofingNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "打样申请单号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1CreateDate',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "createDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "创建时间",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1CreateBy',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "createBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "创建人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1UpdateDate',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "updateDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "上次修改日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1UpdateBy',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "updateBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "修改人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 103002,
            serialNumber: '103002Table1ecnAddress',
            tableId: "103002Table1",
            tableName: "技术参数卡列表",
            columnProp: "ecnAddress",
            headerAlign: "center",
            align: "left",
            columnLabel: "ECN文档链接",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 200
          },
        ],
        dataForm:{
          id:'',
          site: '',
          projectId: '',
          testPartNo:'',
          partType:'',
          partTypeDesc:'',
          partName:'',
          partDesc:'',
          partSpec:'',
          remark:'',
          materialNumber:'',
          username:'',
        },
        dataRole: {
          partTypeDesc: [
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
          partName: [
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
        },
        // 导出 start
        exportData: [],
        exportName: '技术参数卡清单'+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["技术参数卡清单"],
        exportFooter: [],
        // 导出 end
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = window.innerHeight - 520;
      })
    },
    methods: {
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.search()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.search()
      },
      search(){
        this.searchData.limit = this.pageSize
        this.searchData.page = this.pageIndex
        technicalSpecificationListSearch(this.searchData).then(({data}) => {
          if (data.code == 0) {
            this.dataList1 = data.page.list
            this.pageIndex = data.page.currPage
            this.pageSize = data.page.pageSize
            this.totalPage = data.page.totalCount
          }
          this.dataListLoading = false
        })
      },
      // 获取基础数据列表S
      getBaseList (val, type) {
        this.tagNo = val
        this.tagNo1 = type
        this.$nextTick(() => {
          let strVal = ''
          if (val === 1013) {
            if(type==1) {
              strVal = this.dataForm.partType
            }
          }
          this.$refs.baseList.init(val, strVal)
        })
      },
      /* 列表方法的回调 */
      getBaseData (val) {
        if (this.tagNo === 1013) {
          if(this.tagNo1==1) {
            this.dataForm.partType = val.Base_id
            this.dataForm.partTypeDesc = val.Base_desc
          }
        }
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      addModal(){
        if(this.searchData.projectId==""||this.searchData.projectId==null){
          this.$alert('请选择项目！', '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        this.dataForm={
          id:0,
          site: this.searchData.site,
          projectId: this.searchData.projectId,
          testPartNo:'',
          partType:'',
          partTypeDesc:'',
          partName:'',
          partDesc:'',
          partSpec:'',
          remark:'',
          materialNumber:'',
          username:this.$store.state.user.name,
        }
        this.visible=true;
      },
      updateModel(row){
        this.dataForm={
          id:row.id,
          site: row.site,
          projectId: row.projectId,
          testPartNo:row.testPartNo,
          partType:row.partType,
          partTypeDesc:row.partTypeDesc,
          partName:row.partName,
          partDesc:row.partDesc,
          partSpec:row.partSpec,
          remark:row.remark,
          materialNumber:row.materialNumber,
          username:this.$store.state.user.name,
        }
        this.visible=true;
      },
      saveData(){
        if(this.dataForm.partType==''||this.dataForm.partType==null){
          this.$alert('请选择物料类别！', '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if(this.dataForm.partName==''||this.dataForm.partName==null){
          this.$alert('请输入物料名称！', '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        saveProjectPart(this.dataForm).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success( '操作成功')
            this.visible = false
            this.searchTable();
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      deleteData(){
        this.$confirm('确定要删除这条数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteProjectPartWithId(this.dataForm).then(({data}) => {
            if (data && data.code === 0) {
              this.$message.success( '操作成功')
              this.searchTable();
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {
        })
      },
      //导出excel
      createExportData() {

        return this.dataList;

      },
      startDownload() {
        //  this.exportData = this.dataList

      },
      finishDownload() {
      },
      fields() {
        let json = "{"
        this.columnList1.forEach((item, index) => {
          if (index == this.columnList1.length - 1) {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
          } else {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
          }
        })
        json += "}"
        let s = eval("(" + json + ")")

        return s
      },
    },
  }
</script>

<style scoped>

</style>
