<template>
  <div class="mod-config">
    <el-form label-position="top" style="margin-top: 1px; margin-left: 0px;">
      <el-form :inline="true" label-position="top" style="margin-top: 0px">
        <el-button type="primary" @click="searchTable">刷新</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>
    </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>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      width="320px"
      :title="!dataForm.id ?  '添加' :'编辑'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :inline="true" label-position="top" label-width="100px" >
        <el-form-item :label="'工厂编码'">
          <el-input v-model="dataForm.site" style="width: 130px" disabled></el-input>
        </el-form-item>

        <el-form-item :label="'加工中心编码'">
          <el-input v-model="dataForm.workCenterNo" style="width: 130px" disabled></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px" >
        <el-form-item :label="'模板'"  required="required">
          <el-select v-model="dataForm.itemType" style="width: 130px" placeholder="请选择">
            <el-option v-for="item in itemList" :key="index" :label="item.typeDesc" :value="item.itemType"></el-option>
          </el-select>


        </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>
    <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import {
    searchWorkCenterBMType,
    getPlmRouteItemType,
    updateWorkCenterBMType,
  } from "@/api/base/site.js"
  import Chooselist from '@/views/modules/common/Chooselist'
  export default {
    components: {
      Chooselist
    },
    data() {
      return {
        height:300,
        dataList: [],
        searchData: {
          site:this.$store.state.user.site
        },
        exportList:[],
        visible:false,
        dataListLoading: false,
        currentRow:'',
        columnList1:[
          {
            userId: this.$store.state.user.name,
            functionId: 100003001,
            serialNumber: '100003001Table1Site',
            tableId: "100003001Table1",
            tableName: "加工中心BM组件",
            columnProp: "site",
            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: 100003001,
            serialNumber: '100003001Table1WorkCenterNo',
            tableId: "100003001Table1",
            tableName: "加工中心BM组件",
            columnProp: "workCenterNo",
            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: 100003001,
            serialNumber: '100003001Table1WorkCenterDesc',
            tableId: "100003001Table1",
            tableName: "加工中心BM组件",
            columnProp: "workCenterDesc",
            headerAlign: "left",
            align: "left",
            columnLabel: "加工中心名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },{
            userId: this.$store.state.user.name,
            functionId: 100003001,
            serialNumber: '100003001Table1TypeDesc',
            tableId: "100003001Table1",
            tableName: "加工中心BM组件",
            columnProp: "typeDesc",
            headerAlign: "left",
            align: "left",
            columnLabel: "BenchMark模板",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          }
        ],
        dataForm:{
          id:'',
          site: '',
          workCenterNo: '',
          itemType:'',
        },

        // 导出 start
        exportData: [],
        exportName: '加工中心BM组件'+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["加工中心BM组件"],
        exportFooter: [],
        // 导出 end
        itemList:[],
      }
    },
    created() {
      this.searchTable()
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 180
      })
    },
    methods: {
      // 获取基础数据列表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
          }
        }
      },
      searchTable(){
        searchWorkCenterBMType(this.searchData).then(({data}) => {
          //区分请求成功和失败的状况
          if (data && data.code == 0) {
            this.dataList = data.rows;
          } else {
            this.dataList = [];
          }
        });
      },
      updateModel(row){
        this.dataForm={
          site: row.site,
          workCenterNo:row.workCenterNo,
          itemType: row.itemType,
        }
        getPlmRouteItemType(this.dataForm).then(({data})=>{
          this.itemList=data.rows
        })
        this.visible=true;
      },
      saveData(){
        if(this.dataForm.itemType==''||this.dataForm.itemType==null){
          this.$alert('请选择模板！', '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        updateWorkCenterBMType(this.dataForm).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success( '操作成功')
            this.visible = false
            this.searchTable();
          } else {
            this.$message.error(data.msg)
          }
        })
      },

      //导出excel
      //导出excel
      async 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>
