<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'制造商编码'">
        <el-input v-model="searchData.manufacturerId" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'制造商名称'">
        <el-input v-model="searchData.manufacturerName" clearable style="width: 210px"></el-input>
      </el-form-item>
      <el-form-item :label="'是否在用'">
        <el-select clearable v-model="searchData.active" style="width: 120px">
          <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">
          <el-link style="cursor: pointer" @click="updateModal(scope.row)">修改</el-link>
          <el-link style="cursor: pointer" @click="delModal(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <el-pagination style="margin-top: 0px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 200, 500]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog :title="modalData.title" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="495px">
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="制造商编码" prop="manufacturerId" :rules="rules.manufacturerId">
          <el-input v-model="modalData.manufacturerId" :disabled="modalDisableFlag" style="width: 140px"></el-input>
        </el-form-item>
        <el-form-item label="制造商名称" prop="manufacturerName" :rules="rules.manufacturerName">
          <el-input v-model="modalData.manufacturerName" style="width: 302px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="是否在用" prop="active" :rules="rules.active">
          <el-select v-model="modalData.active" style="width: 140px">
            <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:30px;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>

    <!-- chooseList模态框 -->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>


<script>
import {
  manufacturerInformationSearch, // 制造商信息列表查询
  manufacturerInformationSave, // 制造商信息新增
  manufacturerInformationEdit, // 制造商信息编辑
  manufacturerInformationDelete // 制造商信息删除
} from '@/api/part/manufacturerInformation.js'
import Chooselist from '@/views/modules/common/Chooselist'
import manufacturerInformation from './manufacturerInformation'
export default {
  components: {
    Chooselist
  },
  watch: {
    searchData: {
      deep: true,
      handler: function (newV, oldV) {
        this.searchData.manufacturerId = this.searchData.manufacturerId.toUpperCase()
      }
    },
    modalData: {
      deep: true,
      handler: function (newV, oldV) {
        this.modalData.manufacturerId = this.modalData.manufacturerId.toUpperCase()
      }
    }
  },
  data () {
    return {

      // 导出
      exportData: [],
      exportName: '制造商' + this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: ['制造商'],
      exportFooter: [],
      resultList: [],
      // ======== 行高 ========
      height: 200,
      // ======== 分页 ========
      pageIndex: 1,
      pageSize: 50,
      totalPage: 0,
      // 条件查询
      searchData: {
        site: this.$store.state.user.site,
        manufacturerId: '',
        manufacturerName: '',
        active: '',
        page: 1,
        limit: 10
      },
      // 其它
      dataListLoading: false,

      modalData: {
        flag: '',
        title: '',
        site: this.$store.state.user.site,
        manufacturerId: '',
        manufacturerName: '',
        active: ''
      },
      // ======== 数据列表 ========
      dataList: [],
      // 展示列集
      columnList: [
        {
          userId: this.$store.state.user.name,
          functionId: 104007,
          serialNumber: '104007Table1ManufacturerId',
          tableId: "104007Table1",
          tableName: "制造商信息表",
          columnProp: 'manufacturerId',
          headerAlign: "center",
          align: "center",
          columnLabel: '制造商编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104007,
          serialNumber: '104007Table1ManufacturerName',
          tableId: "104007Table1",
          tableName: "制造商信息表",
          columnProp: 'manufacturerName',
          headerAlign: "center",
          align: "left",
          columnLabel: '制造商名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 300
        },
        {
          functionId: 104007,
          serialNumber: '104007Table1Active',
          tableId: '104007Table1',
          tableName: '制造商信息表',
          columnProp: 'active',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '是否在用',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104007,
          serialNumber: '104007Table1CreateDate',
          tableId: '104007Table1',
          tableName: '制造商信息表',
          columnProp: 'createDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 170
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104007,
          serialNumber: '104007Table1CreateBy',
          tableId: "104007Table1",
          tableName: "制造商信息表",
          columnProp: 'createBy',
          headerAlign: "center",
          align: "center",
          columnLabel: '创建人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104007,
          serialNumber: '104007Table1UpdateDate',
          tableId: "104007Table1",
          tableName: "制造商信息表",
          columnProp: 'updateDate',
          headerAlign: "center",
          align: "center",
          columnLabel: '更新时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 170
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104007,
          serialNumber: '104007Table1UpdateBy',
          tableId: "104007Table1",
          tableName: "制造商信息表",
          columnProp: 'updateBy',
          headerAlign: "center",
          align: "center",
          columnLabel: '更新人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
      ],
      rules:{
        manufacturerId:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        manufacturerName:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        active:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
      },
      // ======== 模态框开关控制 ========
      modalFlag: false,
      modalDisableFlag: false,
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.height = window.innerHeight - 170
    })
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },

    //导出excel
    async createExportData() {
      this.searchData.limit = -1
      this.searchData.page = 1
      await manufacturerInformationSearch(this.searchData).then(({data}) => {
        this.exportList= data.page.list;
      })
      return this.exportList;
    },
    startDownload() {

    },
    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
    // 获取数据列表
    getDataList () {
      this.searchData.limit = this.pageSize
      this.searchData.page = this.pageIndex
      manufacturerInformationSearch(this.searchData).then(({data}) => {
        if (data.code === 0) {
          this.dataList = data.page.list
          this.pageIndex = data.page.currPage
          this.pageSize = data.page.pageSize
          this.totalPage = data.page.totalCount
        }
        this.dataListLoading = false
      })
    },

    addModal(){
      this.modalData = {
        flag: '1',
        title: '制造商新增',
        site: this.$store.state.user.site,
        createBy: this.$store.state.user.name,
        manufacturerId: '',
        manufacturerName: '',
        active: 'Y',
      }
      this.modalDisableFlag = false
      this.modalFlag = true
    },
    /**
     * 制造商信息编辑模态框
     * @param row
     */
    updateModal (row) {
      this.modalData = {
        flag: '2',
        title: '制造商编辑',
        site: row.site,
        manufacturerId: row.manufacturerId,
        manufacturerName: row.manufacturerName,
        active: row.active,
        updateBy: this.$store.state.user.name,
      }
      this.modalDisableFlag = true
      this.modalFlag = true
    },

    // ======== 新增/编辑/删除方法 ========
    /**
     * 制造商信息新增/编辑
     */
    saveData () {
      if (this.modalData.manufacturerId === '' || this.modalData.manufacturerId == null) {
        this.$message.warning('请填写制造商编码！')
        return
      }
      if (this.modalData.manufacturerName === '' || this.modalData.manufacturerName == null) {
        this.$message.warning('请填写制造商名称！')
        return
      }
      if (this.modalData.active === '' || this.modalData.active == null) {
        this.$message.warning('请选择是否可用！')
        return
      }
      if (this.modalData.flag === '1') {
        manufacturerInformationSave(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 {
        manufacturerInformationEdit(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: '确定'
            })
          }
        })
      }
    },
    /**
     * 制造商信息删除
     */
    delModal (row) {
      this.$confirm(`是否删除这条制造商信息?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        manufacturerInformationDelete(row).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.partSelections = []
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      }).catch(() => {
      })
    },
  }
}
</script>

<style scoped lang="scss">
/deep/ .customer-tab .el-tabs__content {
  padding: 0px !important;
  height: 459px;
}
</style>
