<template>
  <div class="mod-config">
    <!-- 收藏 -->
    <div>
        <span @click="favoriteFunction()">
         <icon-svg :name="favorite?'xiangqufill':'xiangqu'" class="sl-svg"></icon-svg>
        </span>
    </div>
    <!-- 条件查询 -->
    <el-form :inline="true"  label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'质检员工号'">
        <el-input v-model="searchData.inspectorNo"  style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'质检员姓名'">
        <el-input v-model="searchData.inspectorName"  style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'所处检验工序'">
        <el-input v-model="searchData.inspectorType"  style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'是否在岗'">
        <el-select filterable v-model="searchData.inspectorActive" style="width: 120px">
          <el-option label="请选择" value=""></el-option>
          <el-option label="是" value="Y"></el-option>
          <el-option label="否" value="N"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item  :label="' '">
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button  type="primary" @click="addModal()">新增</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
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <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-column
        fixed="right"
        header-align="center"
        align="center"
        width="160"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="updateModal(scope.row)">修改</a>
          <a type="text" size="small" @click="deleteModal(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增和修改 -->
    <el-dialog title="质检员信息" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="430px">
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="质检员工号:">
          <el-input v-model="modalData.inspectorNo" :disabled="modalDisableFlag" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="质检员姓名:">
          <el-input v-model="modalData.inspectorName"  style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="质检员电话:">
          <el-input v-model="modalData.inspectorPhone" type="number" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'所处检验工序'">
          <el-input v-model="modalData.inspectorType"  style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'是否在岗'">
          <el-select filterable v-model="modalData.inspectorActive" style="width: 120px">
            <el-option label="是" value="Y"></el-option>
            <el-option label="否" value="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="saveData()">保存</el-button>
        <el-button type="primary" @click="modalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>
  import {
    inspectorSearch, // 搜索质检员信息
    inspectorSave, // 新增质检员信息
    inspectorUpdate, // 修改质检员信息
    inspectorDelete, // 删除质检员信息
  } from "@/api/qc/qc.js"
  import {userFavoriteList, saveUserFavorite, removeUserFavorite} from '@/api/userFavorite.js'
  export default {
    watch: {
      searchData: {
        deep: true,
        handler: function (newV, oldV) {
          this.searchData.inspectorNo = this.searchData.inspectorNo.toUpperCase()
        }
      },
      modalData: {
        deep: true,
        handler: function (newV, oldV) {
          this.modalData.inspectorNo = this.modalData.inspectorNo.toUpperCase()
          this.modalData.inspectorType = this.modalData.inspectorType.toUpperCase()
        }
      }
    },
    data () {
      return {
        // 是否收藏
        favorite: false,

        // 导出 start
        exportData: [],
        exportName: "质检员" + this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["质检员"],
        exportFooter: [],
        exportList:[],
        // 导出 end

        searchData: {
          inspectorNo: '',
          inspectorName: '',
          inspectorPhone: '',
          inspectorType: '',
          inspectorActive: ''
        },
        height: 200,
        dataList: [],
        dataListLoading: false,
        modalFlag: false,
        modalDisableFlag: false,
        modalData: {
          flag: '',
          inspectorNo: '',
          inspectorName: '',
          inspectorPhone: '',
          inspectorType: '',
          inspectorActive: ''
        },
        // 标头展示
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableEamPropertiesItemID',
            tableId: '101008Table',
            tableName: 'common',
            columnProp: 'inspectorNo',
            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: 101008,
            serialNumber: '101008TableEamPropertiesItemDesc',
            tableId: '101008Table',
            tableName: 'common',
            columnProp: 'inspectorName',
            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: 101008,
            serialNumber: '101008TableEamPropertiesItemDesc',
            tableId: '101008Table',
            tableName: 'common',
            columnProp: 'inspectorPhone',
            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: 101008,
            serialNumber: '101008TableEamPropertiesItemDesc',
            tableId: '101008Table',
            tableName: 'common',
            columnProp: 'inspectorType',
            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: 101008,
            serialNumber: '101008TableEamPropertiesItemDesc',
            tableId: '101008Table',
            tableName: 'common',
            columnProp: 'inspectorActive',
            headerAlign: 'center',
            align: 'left',
            columnLabel: '是否在岗',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 140
          }
        ]
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 180
      })
    },
    created () {
      this.getDataList()
      this.favoriteIsOk()
    },
    methods: {
      // 查询获取数据列表
      getDataList () {
        inspectorSearch(this.searchData).then(({data}) => {
          this.dataList = data.rows
        })
      },
      // 新增按钮
      addModal () {
        this.modalData = {
          flag: '1',
          inspectorNo: '',
          inspectorName: '',
          inspectorPhone: '',
          inspectorType: '',
          inspectorActive: ''
        }
        this.modalDisableFlag = false
        this.modalFlag = true
      },
      // 修改按钮
      updateModal (row) {
        this.modalData = {
          flag: '2',
          inspectorNo: row.inspectorNo,
          inspectorName: row.inspectorName,
          inspectorPhone: row.inspectorPhone,
          inspectorType: row.inspectorType,
          inspectorActive: row.inspectorActive
        }
        this.modalDisableFlag = true
        this.modalFlag = true
      },
      // 删除方法
      deleteModal (row) {
        this.$confirm(`是否删除这个质检员信息?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          inspectorDelete(row).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },
      // 新增/修改方法
      saveData () {
        if (this.modalData.inspectorNo == '' || this.modalData.inspectorNo == null) {
          this.$alert('请输入质检员工号！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.inspectorName == '' || this.modalData.inspectorName == null) {
          this.$alert('请输入质检员姓名！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.inspectorPhone == '' || this.modalData.inspectorPhone == null) {
          this.$alert('请输入质检员电话！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.inspectorType == '' || this.modalData.inspectorType == null) {
          this.$alert('请输入质检员职责！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.inspectorActive == '' || this.modalData.inspectorActive == null) {
          this.$alert('请选择质检员是否在岗！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.flag == '1') { // 新增
          inspectorSave(this.modalData).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.modalFlag = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        } else { // 修改
          inspectorUpdate(this.modalData).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.modalFlag = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }
      },

      // 校验用户是否收藏
      favoriteIsOk() {
        let userFavorite = {
          userId: this.$store.state.user.id,
          languageCode: this.$i18n.locale
        }
        console.log(this.$route.meta.menuId)
        userFavoriteList(userFavorite).then(({data}) => {
          for (let i = 0; i < data.list.length; i++) {
            // let flag=false;
            if(this.$route.meta.menuId==data.list[i].menuId){
              this.favorite = true
              // flag=true;
            }
          }

        })
      },
      // 收藏 OR 取消收藏
      favoriteFunction() {
        let userFavorite = {
          userId: this.$store.state.user.id,
          functionId: this.$route.meta.menuId,
        }
        if (this.favorite) {
          // 取消收藏
          // this.$confirm(`确定取消收藏`, '提示', {
          //   confirmButtonText: '确定',
          //   cancelButtonText: '取消',
          //   type: 'warning'
          // }).then(() => {
            removeUserFavorite(userFavorite).then(({data}) => {
              this.$message.success(data.msg)
              this.favorite = false
            })
          // })
        } else {
          // 收藏
          saveUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = true
          })
        }
      },

      //导出excel
      async createExportData() {
        this.searchData.limit = -1
        this.searchData.page = 1
        await inspectorSearch(this.searchData).then(({data}) => {
          this.exportList= data.rows;
        })

        return this.exportList;
      },
      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
    }
  }
</script>
