<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.methodName"  style="width: 120px"></el-input>
      </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.methodName"  style="width: 120px"></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-input v-model="modalData.methodRemark"  style="width: 254px"></el-input>
        </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 {
    qcMethodSearch, // 检验方法查询
    qcMethodSave, // 检验方法新增
    qcMethodUpdate, // 检验方法修改
    qcMethodDelete, // 检验方法删除
  } from "@/api/qc/qc.js"
  import {userFavoriteList, saveUserFavorite, removeUserFavorite} from '@/api/userFavorite.js'
  export default {
    watch: {

    },
    data () {
      return {
        // 是否收藏
        favorite: false,
        // 导出 start
        exportData: [],
        exportName: "检验方法" + this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["检验方法"],
        exportFooter: [],
        exportList:[],
        // 导出 end
        searchData: {
          methodNo: '',
          methodName: '',
        },
        height: 200,
        dataList: [],
        dataListLoading: false,
        modalFlag: false,
        modalDisableFlag: false,
        methodData: {
          methodNo: '',
          valueNo: '',
          availableValue: '',
          createBy: this.$store.state.user.name
        },
        modalData: {
          flag: '',
          methodNo: '',
          methodName: '',
          createBy: this.$store.state.user.name,
          methodRemark: '',
        },
        // 标头展示
        columnList: [
          {
            columnProp: 'methodNo',
            headerAlign: 'center',
            align: 'left',
            columnLabel: '方法编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
          {
            columnProp: 'methodName',
            headerAlign: 'center',
            align: 'left',
            columnLabel: '方法名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
          {
            columnProp: 'methodRemark',
            headerAlign: 'center',
            align: 'left',
            columnLabel: '方法说明',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
          {
            columnProp: 'createDate',
            headerAlign: 'center',
            align: 'left',
            columnLabel: '创建时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
          {
            columnProp: 'createBy',
            headerAlign: 'center',
            align: 'left',
            columnLabel: '创建人',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
        ]
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 180
      })
    },
    created () {
      this.getDataList()
      this.favoriteIsOk()
    },
    methods: {
      // 查询获取数据列表
      getDataList () {
        qcMethodSearch(this.searchData).then(({data}) => {
          this.dataList = data.rows
        })
      },
      // 新增按钮
      addModal () {
        this.modalData = {
          flag: '1',
          createBy: this.$store.state.user.name,
          methodNo: '',
          methodName: '',
          methodRemark: '',
        }
        this.modalDisableFlag = false
        this.modalFlag = true
      },
      // 修改按钮
      updateModal (row) {
        this.modalData = {
          flag: '2',
          createBy: this.$store.state.user.name,
          methodNo: row.methodNo,
          methodName: row.methodName,
          methodRemark: row.methodRemark,
        }
        this.modalDisableFlag = true
        this.modalFlag = true
      },
      // 删除方法
      deleteModal (row) {
        this.$confirm(`是否删除这个检验方法?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          qcMethodDelete(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.methodName == '' || this.modalData.methodName == null) {
          this.$alert('请输入检验方法名称！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.flag == '1') { // 新增
          qcMethodSave(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 { // 修改
          qcMethodUpdate(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 qcMethodSearch(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>
