<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-button @click="getData()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'查询'}}</el-button>
      <el-button @click="addModal()" 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">
        {{ buttons.download }}
      </download-excel>
    </el-form>
    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="editModal(scope.row)">编辑</a>
          <a type="text" size="small" @click="deleteModal(scope.row)">删除</a>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList" :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>
    <el-dialog title="班次信息维护" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="470px" >
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label="'工厂编码：'">
          <el-input v-model="modalData.site" :disabled="InputFlag" style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'班次编码：'">
          <el-input v-model="modalData.shiftNo" :disabled="InputFlag" style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'班次名称：'">
          <el-input v-model="modalData.shiftDesc"  style="width: 130px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label="'上班时间：'">
          <el-time-picker
            format="HH:mm"
            style="width: 130px"
            v-model="modalData.startexacttime"
            placeholder="请选择"
            value-format="HH:mm">
          </el-time-picker>
        </el-form-item>
        <el-form-item :label="'下班时间：'">
          <el-time-picker
            format="HH:mm"
            style="width: 130px"
            v-model="modalData.endexacttime"
            placeholder="请选择"
            value-format="HH:mm">
          </el-time-picker>
        </el-form-item>
      </el-form>
      <el-footer  style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="modalSave()">保存</el-button>
        <el-button type="primary" @click="modalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>

  import {
    getShiftData,
    shiftSave,
    deleteShift,
  } from "@/api/base/site.js"
  import {
    searchSysLanguagePackList,
    searchSysLanguageParam,
    searchFunctionButtonList,
    saveButtonList,
    searchSysLanguage,
    searchLanguageListByLanguageCode,
    saveSysLanguageOne,
    searchPageLanguageData,
    removerLanguage
  } from "@/api/sysLanguage.js"
  import {
    saveTableDefaultList,
    saveTableUser,
    getTableDefaultListLanguage,
    getTableUserListLanguage,
    removerDefault,
    removerUser
  } from "@/api/table.js"
  import Chooselist from '@/views/modules/common/Chooselist'

  export default {
    name: "factoryInformation",
    components: {
      Chooselist
    },
    data() {
      return {
        tagNo:'',
        // 导出 start
        exportData: [],
        exportName: "班次信息"+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["班次信息"],
        exportFooter: [],
        // 导出 end
        // 多语言 start
        buttonList: [

        ],
        showDefault:false,
        // 语言词典集合
        sysLanguageParams: [],
        dataListLoading: false,
        // 用户table 配置集合
        userColumnList: [],
        queryTableUser: {
          userId: this.$store.state.user.name,
          functionId: this.$route.meta.menuId,
          tableId: "100003Table",
          status: true,
          languageCode: this.$i18n.locale
        },
        buttons:{
          defaultTable:'设置默认配置',
          search:'查询',
          download:'导出',
          settingTable: '设置列表',
          site:'工厂编码',
          active:'在用',
          all:'全部',
          yes:'是',
          no:'否',
          partNo:'物料编码',
          partDescription:'物料名称',
          spec:'规则型号',
          newDate:'新增日期',
          to:'至',
        },
        // 默认table 查询参数
        queryTable: {
          functionId: this.$route.meta.menuId,
          tableId: "100003Table",
          languageCode: this.$i18n.locale
        },
        languageList: [],
        queryLanguage: {},
        visible:false,
        // 多语言 end
        height: 200,
        dataList:[],
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 100003,
            serialNumber: '100003TableSite',
            tableId: "100003Table",
            tableName: "工厂信息表",
            columnProp: "site",
            headerAlign: "center",
            align: "left",
            columnLabel: "工厂编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 60
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100003,
            serialNumber: '100003TableShiftNo',
            tableId: "100003Table",
            tableName: "工厂信息表",
            columnProp: "shiftNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "班次编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 60
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100003,
            serialNumber: '100003TableShiftDesc',
            tableId: "100003Table",
            tableName: "工厂信息表",
            columnProp: "shiftDesc",
            headerAlign: "center",
            align: "left",
            columnLabel: "班次名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100003,
            serialNumber: '100003TableStartexactTime',
            tableId: "100003Table",
            tableName: "工厂信息表",
            columnProp: "startexacttime",
            headerAlign: "center",
            align: "left",
            columnLabel: "开始时间",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100003,
            serialNumber: '100003TableEndexactTime',
            tableId: "100003Table",
            tableName: "工厂信息表",
            columnProp: "endexacttime",
            headerAlign: "center",
            align: "left",
            columnLabel: "结束时间",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
        ],
        modalData:{
          flag:0,
          site:'',
          shiftNo:'',
          shiftDesc:'',
          startexacttime:'',
          endexacttime:'',
        },
        modalFlag:false,
        InputFlag:false,
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = window.innerHeight - 140;
      })
    },
    watch: {
      '$route' (to, from) {
        if(localStorage.getItem('factoryInformation')!=undefined){
          this.getData();
        }
      },
      modalData: {
        deep: true,
        handler: function (newV, oldV) {
          this.modalData.shiftNo = this.modalData.shiftNo.toUpperCase()
        }
      },
    },
    methods: {
      getBaseData(val){

        this.$nextTick(() => {
          if (this.tagNo === 93) {
            this.modalData.companyID = val.CompanyID
          }
        })

      },
      // 获取基础数据列表
      getBaseList(val){
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = "";
          if (val === 93 &&  this.modalData.companyID){
            strVal = this.modalData.companyID
          }
          this.$refs.baseList.init(val,strVal)
        })
      },
      getData(){
        // let data={};
        // if(localStorage.getItem('factoryInformation')!=undefined){
        //   data=JSON.parse(localStorage.getItem('factoryInformation'));
        // }
        // localStorage.removeItem('factoryInformation');
        let data={
          site:this.$store.state.user.site
        }
        getShiftData(data).then(({data}) => {
          this.dataList = data.rows
        })
      },
      addModal(){
        this.modalData.flag=0;
        this.modalData.site=this.$store.state.user.site;
        this.modalData.shiftNo='';
        this.modalData.shiftDesc='';
        this.modalData.startexacttime='';
        this.modalData.endexacttime='';
        this.InputFlag=false;
        this.modalFlag=true;
      },
      editModal(row){
        this.modalData.flag=1;
        this.modalData.site=row.site;
        this.modalData.shiftNo=row.shiftNo;
        this.modalData.shiftDesc=row.shiftDesc;
        this.modalData.startexacttime=row.startexacttime;
        this.modalData.endexacttime=row.endexacttime;
        this.InputFlag=true;
        this.modalFlag=true;
      },
      modalSave(){
        if(this.modalData.site==''||this.modalData.site==null){
          this.$alert("请选择工厂编码！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modalData.shiftNo==''||this.modalData.shiftNo==null){
          this.$alert("请输入班次编码！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modalData.shiftDesc==''||this.modalData.shiftDesc==null){
          this.$alert("请输入班次名称！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modalData.startexacttime==''||this.modalData.startexacttime==null){
          this.$alert("请输入上班时间！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modalData.endexacttime==''||this.modalData.endexacttime==null){
          this.$alert("请输入下班时间！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        shiftSave(this.modalData).then(({data}) => {

          if (data && data.code == 0) {
            this.modalFlag = false
            this.getData();
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      deleteModal(row){
        this.$confirm(`是否删除此条班次记录?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let daleteData={
            site:row.site,
            shiftNo:row.shiftNo
          }
          deleteShift(daleteData).then(({data}) => {
            if (data && data.code == 0) {
              this.modelFlag = false
              this.getData();
              this.$message.success('操作成功')
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },



      //导出excel
      createExportData() {

        return this.dataList;

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

      },
      finishDownload() {

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

        return s
      },
      // 导出 end
      getCompanyInformation(row){
        let inData={companyID:row.companyID,companyName:row.companyName};
        localStorage.setItem('companyInformation', JSON.stringify(inData))
        this.$router.replace('base-companyInformation')
      },
      //多语言start
      // 获取页面多语言
      getMsgLanguage() {
        this.dataListLoading = true
        let queryLang = {
          functionId: this.$route.meta.menuId,
          table_id: '100003Table',
          languageCode: this.$i18n.locale
        }
        searchPageLanguageData(queryLang).then((data) => {
          if (JSON.stringify(data.data) != '{}') {
            this.pageLanguageData = data.data
          } else {
          }
        })
        this.dataListLoading = false
      },
      // 获取button的词典
      getFunctionButtonList() {
        searchFunctionButtonList(this.queryTable).then(({data}) => {
          if (JSON.stringify(data.data) != '{}') {
            this.buttons = data.data
          } else {
            // saveButtonList(this.buttonList).then(({data}) => {
            // })
          }
        })
      },
      // 获取语言词典
      getSysLanguageParamList() {
        searchSysLanguageParam(this.queryTable).then(({data}) => {
          this.sysLanguageParams = data.rows
        })
      },
      // 打开页面设置
      userSetting() {
        this.visible = true;
        let queryTable = {
          userId: this.$store.state.user.name,
          functionId: this.$route.meta.menuId,
          tableId: "100003Table",
          languageCode: this.$i18n.locale
        }
        this.$nextTick(() => {
          this.$refs.column.init(queryTable);
        });
      },
      // 获取 用户保存的 格式列
      getTableUserColumn() {
        getTableUserListLanguage(this.queryTableUser).then(({data}) => {
          if (data.rows.length>0) {
            //this.columnList = []
            this.columnList = data.rows
          } else {
            this.getColumnList()
          }

        })
      },
      // 保存 默认配置 列
      saveColumnList() {
        // 删除所有的该页面下保存的数据
        if (this.userColumnList) {
          //删除 user自定义的数据
          removerUser(this.queryTable)
        }
        // 删除默认配置
        removerDefault(this.queryTable)
        // 删除语言
        removerLanguage(this.queryTable)
        // 保存页面 table属性
        let sumColumnList = this.columnList;
        saveTableDefaultList(sumColumnList).then(({data}) => {
        })
        // 保存页面 button label title 属性
        saveButtonList(this.buttonList).then(({data}) => {
        })
        this.getFunctionButtonList()
        this.getColumnList()
      },
      // 获取 tableDefault 列
      getColumnList() {
        getTableDefaultListLanguage(this.queryTable).then(({data}) => {
          if (data.rows.length>0) {
            // this.showDefault = false
            this.columnList = data.rows
          } else {
            // this.showDefault = true
          }
        })
      },
      //多语言 end
    },
    created() {
      this.getData();
    }
  }
</script>

<style >
  .el-textarea__inner {
    padding: 5px 5px;
  }
</style>
