<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="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" 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-tag v-if="scope.row.确认时间" type="success" size="small">已确认</el-tag>
              <el-tag v-if="!scope.row.确认时间" type="info" size="small">未确认</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" min-width="200">
            <template slot-scope="scope">
              <el-button :disabled="!!scope.row.确认时间" size="mini" type="success" plain icon="el-icon-success" @click="Received(scope.row)">确认</el-button>
              <el-button size="mini" type="success" plain icon="el-icon-success" @click="openAdopt(scope.row)">通过</el-button>
              <el-button size="mini" type="success" plain icon="el-icon-error" @click="editType('form', 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="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="专家">
          <el-upload
            ref="upload"
            :action="action"
            :on-success="uploadSuccess"
            :on-error="uploadFailure"
            :on-preview="handlePictureCardPreview"
            :file-list="fileList"
            :auto-upload="false"
            :limit="parseInt(1)"
            multiple
            list-type="picture-card">
            <i class="el-icon-plus"/>
          </el-upload>
        </el-form-item>
        <el-form-item label="问题描述:" prop="问题描述">
          <el-input v-model="form.问题描述" :rows="2" type="textarea" size="small" style="width: 75%" clearable placeholder="请输入问题描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submitUpload('form')" >确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible2">
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible3">
      <img :src="dialogImageUrl3" width="100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { ExecDatabase } from '@/main'
import { mapGetters } from 'vuex'
import { ImageUrl } from '@/utils/request'
import { uploadFile2 } from '@/utils/request'
export default {
  data() {
    return {
      EnterpriseValue: '',
      Enterprise: [], // 企业
      ProductValue: '',
      Product: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1, // 后台获取页
      dialogFormVisible: false,
      form: {
        问题描述: ''
      },
      rules: {
        问题描述: [{ required: true, message: '请输入问题描述', trigger: 'blur' }]
      },
      dialogVisible2: false,
      bqliushuihao: '',
      dialogImageUrl: '',
      action: '', // ↓
      upload: uploadFile2,
      fileList: [],
      dialogVisible: false,
      dialogImageUrl3: '',
      dialogVisible3: false,
      disabled: true // 上传相关↑
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id'
    ])
  },
  watch: {
    EnterpriseValue() {
      this.EnterpriseChange()
    }
  },
  created() {
    this.getEnterprise()
    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 = []
      }
    },
    // 查询
    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]
      param[4] = ['专家代码', this.id]
      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.计划完成时间.split(' ')[0],
              确认时间: v.确认时间,
              url: ImageUrl + v.图片保存名称 + '.' + v.suffix,
              审核流水号: v.审核流水号,
              第三方问题反馈: v.第三方问题反馈
            })
          })
        }
        this.total = response.data.total
        this.loading = false
      })
    },
    Received(row) {
      var param = []
      param[0] = ['审核流水号', row.审核流水号]
      var Data = this.CreateData('12', '标签审核_专家确认_编辑数据', param)
      ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('已确认')
          this.searchTable()
        }
      })
    },
    // 通过
    openAdopt(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.$message.success('通过成功')
            this.searchTable()
          } else {
            this.$message.error('通过失败')
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消通过'
          })
        })
      })
    },
    // 点击
    editType(formName, row) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      if (this.$refs['upload'] !== undefined) {
        this.$refs['upload'].clearFiles()
      }
      this.form.问题描述 = ''
      this.bqliushuihao = row.审核流水号
      this.dialogFormVisible = true
    },
    // 图片预览
    showPicture(row) {
      this.dialogImageUrl = row.url
      this.dialogVisible2 = true
    },
    // 上传前检测
    beforeUpload(file) {
      const isJPG = /^image\/.*/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传图片')
      }
      return isJPG
    },
    // 预览图片
    handlePictureCardPreview(file) {
      this.dialogImageUrl3 = file.url
      this.dialogVisible3 = true
    },
    // 上传
    uploadSuccess(res, file, fileList) {
      if (res[0].result === '1') {
        const index = fileList.indexOf(file)
        fileList.splice(index, 1)
      } else {
        this.$message.error(`${file.name}文件上传失败，请检查上传文件是否正确！`)
      }
      if (fileList.length === 0) {
        this.$message.success('提交成功')
      }
    },
    uploadFailure(a, file, fileList) {
      fileList.map(file => {
        this.$message.error(`${file.name}文件上传失败，请检查上传文件是否正确！`)
      })
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 提交
    submitUpload(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.action = ''
          this.action = `${uploadFile2}?&num=${this.bqliushuihao}&tableName=标签检测_专家问题反馈_图片上传`
          var param = []
          param[0] = ['审核流水号', this.bqliushuihao]
          param[1] = ['问题描述', this.form.问题描述]
          var Data = this.CreateData('12', '标签审核_专家问题反馈_编辑数据', param)
          ExecDatabase(Data).then(response => {
            this.$refs.upload.submit()
            if (response.data[0].result === '1') {
              this.$message({
                message: '反馈成功',
                type: 'success'
              })
              this.pageSize = 10
              this.pageList = 1
              this.searchTable()
            } else {
              this.$message.error('反馈失败')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 分页
    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>
