<template>
  <div>
    <el-card>
      <span>企业名称:</span>
      <el-select v-model="EnterpriseValue" size="small" style="width:160px;" clearable placeholder="请选择企业">
        <el-option
          v-for="item in Enterprise"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span style="margin-left: 5px">产品名称:</span>
      <el-select v-model="ProductValue" size="small" style="width:160px;" clearable placeholder="请选择产品">
        <el-option
          v-for="item in Product"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button :disabled="loading" type="success" plain icon="el-icon-search" size="small" style="margin-left: 20px" @click="pageSize=10;pageList=1;searchTable()">查询</el-button>
    </el-card>
    <el-card>
      <div style="margin-top: 5px">
        <el-table
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{background:'#F5F5F5'}"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          stripe
          tooltip-effect="dark"
          style="width: 100%;margin-top: 10px"
          height="659"
          border
          size="mini">
          <el-table-column align="center" label="企业名称" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.企业名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="产品名称" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.产品名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="上传时间" min-width="90" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.上传时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="图片" min-width="90" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="点击预览，如需下载，可右键保存到本地" placement="top">
                <img :src="scope.row.url" style="height: 80px;width: 80px; margin: 2px auto;display: block" @click="showPicture(scope.row)">
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button type="success" size="mini" icon="el-icon-success" @click="editType('form', scope.row)">确认</el-button>
              <el-button type="success" size="mini" icon="el-icon-error" @click="openDelete(scope.row)">打回</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageList"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogVisible2">
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible" title="专家分配" center width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" class="demo-ruleForm" label-width="160px" style="width: 90%;margin: auto">
        <el-form-item label="要分配的专家:" prop="ExpertValue">
          <el-select v-model="form.ExpertValue" multiple size="small" style="width:190px;" clearable placeholder="请选择专家">
            <el-option
              v-for="item in Expert"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="计划完成日期" prop="endTime">
          <el-date-picker v-model="form.endTime" size="small" type="date" style="width: 190px" placeholder="请选择计划完成日期"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')" >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ExecDatabase } from '@/main'
import { ImageUrl } from '@/utils/request'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      EnterpriseValue: '',
      Enterprise: [], // 企业
      ProductValue: '',
      Product: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1, // 后台获取页
      dialogFormVisible: false,
      form: {
        ExpertValue: [],
        endTime: ''
      },
      Expert: [],
      rules: {
        ExpertValue: [{ required: true, message: '请至少选择一个专家', trigger: 'change' }],
        endTime: [{ required: true, message: '请选择完成日期', trigger: 'change' }]
      },
      dialogVisible2: false,
      dialogImageUrl: '',
      bqliushuihao: ''
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id'
    ])
  },
  watch: {
    EnterpriseValue() {
      this.EnterpriseChange()
    }
  },
  created() {
    this.getEnterprise()
    this.getExpert()
    this.searchTable()
  },
  methods: {
    // 初始化企业
    getEnterprise() {
      this.Enterprise = []
      var param = []
      var Data = this.CreateData('11', '基础数据_企业信息_查询数据', param)
      ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Enterprise.push({
            label: response.data[i].企业名称,
            value: response.data[i].企业代码
          })
        }
      })
    },
    EnterpriseChange() {
      if (this.EnterpriseValue) {
        this.getProduct()
      } else {
        this.ProductValue = ''
        this.Product = []
        this.tableData = []
      }
    },
    // 初始化获取产品
    getProduct() {
      this.ProductValue = ''
      this.Product = []
      var param = []
      param[0] = ['企业代码', this.EnterpriseValue]
      var Data = this.CreateData('11', '基础数据_产品名称_查询数据', param)
      ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Product.push({
            label: response.data[i].产品名称,
            value: response.data[i].产品代码
          })
        }
      }).then(() => {
        this.ProductValue = this.Product[0].value
      })
    },
    productChange() {
      if (this.ProductValue) {
        this.searchTable()
      } else {
        this.tableData = []
      }
    },
    getExpert() {
      this.ExpertValue = []
      this.Expert = []
      var Data = this.CreateData('11', '基础数据_专家信息_查询全部')
      ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Expert.push({
            label: response.data[i].企业名称,
            value: response.data[i].企业代码
          })
        }
      })
    },
    // 查询
    searchTable() {
      this.loading = true
      this.tableData = []
      if (this.EnterpriseValue) { this.EnterpriseValue_check = 1 } else { this.EnterpriseValue_check = 0 }
      if (this.ProductValue) { this.ProductValue_check = 1 } else { this.ProductValue_check = 0 }
      var param = []
      param[0] = ['企业代码_check', this.EnterpriseValue_check]
      param[1] = ['企业代码', this.EnterpriseValue]
      param[2] = ['产品代码_check', this.ProductValue_check]
      param[3] = ['产品代码', this.ProductValue]
      var Data = this.CreateData('11', '企业标签上传_查询数据_未审核', param, this.pageSize, this.pageList)
      ExecDatabase(Data).then(response => {
        if (response.data.rows.length !== 0) {
          response.data.rows.map(v => {
            this.tableData.push({
              企业名称: v.企业名称,
              标签流水号: v.标签流水号,
              产品名称: v.产品名称,
              上传时间: v.上传时间,
              url: ImageUrl + v.图片保存名称 + '.' + v.suffix
            })
          })
        }
        this.total = response.data.total
        this.loading = false
      })
    },
    showPicture(row) {
      this.dialogImageUrl = row.url
      this.dialogVisible2 = true
    },
    // 打回
    openDelete(row) {
      this.$confirm('此操作将打回该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['标签流水号', row.标签流水号]
        var Data = this.CreateData('12', '企业标签上传_打回_编辑数据', param)
        ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            if (this.tableData && this.tableData.length === 1 && this.pageList > 1) { // 判断删除的是否是最后一页最后一行，如果是且不是第一页，页数减一
              this.pageList--
            }
            this.searchTable()
          } else {
            this.$message.error('打回失败')
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消打回'
          })
        })
      })
    },
    // 点击
    editType(formName, row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.ExpertValue = []
      this.bqliushuihao = row.标签流水号
      this.dialogFormVisible = true
    },
    // 专家分配
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['标签流水号', this.bqliushuihao]
          param[1] = ['专家代码组', this.form.ExpertValue]
          param[2] = ['计划完成时间', this.form.endTime]
          var Data = this.CreateData('12', '专家分配_新增数据_循环执行', param)
          ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '专家分配成功',
                type: 'success'
              })
              this.pageList = 1
              this.pageSize = 10
              this.searchTable()
            } else {
              this.$message.error('专家分配失败')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>

<style>
  .el-transfer-panel {
    width:300px;
  }
  .el-transfer-panel__body {
    height: 500px;
    width: 300px;
  }
  .el-transfer-panel__list {
    height: 500px;
    width: 300px;
  }
  .el-transfer-panel__list.is-filterable{
    height: 100%;
  }
  .el-card{
    margin-top: 5px;
  }
</style>
