<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="loading3" type="success" plain icon="el-icon-search" size="small" style="margin-left: 20px" @click="pageSize3=10;pageList3=1;searchTable2()">查询</el-button>
    </el-card>

    <el-card>
      <div style="margin-top: 5px">
        <h4 style="margin: 10px;color: black">全部任务：</h4>
        <el-table
          v-loading="loading3"
          :data="tableData3"
          :header-cell-style="{background:'#F5F5F5'}"
          highlight-current-row
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          tooltip-effect="dark"
          style="width: 100%;margin-top: 10px"
          height="500"
          border
          size="mini"
          @row-click="searchTable">
          <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="120" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.上传时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button size="mini" type="success" plain icon="el-icon-success" @click="adopt(scope.row)">通过</el-button>
              <el-button size="mini" type="success" plain icon="el-icon-success" @click="Enterprises('form3', scope.row)">反馈企业</el-button>
              <el-button size="mini" type="success" plain icon="el-icon-success" @click="editType('form', scope.row)">反馈专家</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading3"
            :current-page="pageList3"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize3"
            :total="total3"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange3"
            @current-change="handleCurrentChange3"/>
        </div>
      </div>
    </el-card>

    <el-card>
      <div style="margin-top: 5px">
        <h4 style="margin: 10px;color: black">专家处理：</h4>
        <el-table
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{background:'#F5F5F5'}"
          highlight-current-row
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          tooltip-effect="dark"
          style="width: 100%;margin-top: 10px"
          height="500"
          border
          size="mini"
          @row-click="rowClick">
          <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-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="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-tag v-if="scope.row.是否通过==='1'" type="success" size="small">已通过</el-tag>
              <el-tag v-if="scope.row.是否通过==='2'" type="danger" size="small">未通过</el-tag>
              <el-tag v-if="scope.row.是否通过==='0'" type="info" size="small">未审核</el-tag>
            </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-card>
      <div style="margin-top: 5px">
        <h4 style="margin: 10px;color: black">审核结果：</h4>
        <el-table
          v-loading="loading2"
          :data="tableData2"
          :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="500"
          border
          size="mini">
          <el-table-column align="center" label="反馈图片" min-width="90" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.url===0">——</span>
              <el-tooltip v-else 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="90" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.问题描述 }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading2"
            :current-page="pageList2"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize2"
            :total="total2"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"/>
        </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"
            :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-item label="计划完成时间:" prop="计划完成时间">
          <el-date-picker
            v-model="form.计划完成时间"
            size="small"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            style="width: 75%"
            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="dialogFormVisible3" title="问题反馈" center width="600px">
      <el-form ref="form3" :model="form3" :rules="rules" label-position="left" class="demo-ruleForm" label-width="160px" style="width: 90%;margin: auto">
        <el-form-item label="反馈图片:" prop="专家">
          <el-upload
            ref="upload3"
            :action="action3"
            :on-success="uploadSuccess3"
            :on-error="uploadFailure3"
            :file-list="fileList3"
            :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="form3.问题描述" :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="callOff3('form3')">取消</el-button>
        <el-button type="primary" @click="submitUpload3('form3')" >确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible2">
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { ExecDatabase } from '@/main'
import { mapGetters } from 'vuex'
import { ImageUrl } from '@/utils/request'
import { uploadFile3 } from '@/utils/request'
import { uploadFile4 } from '@/utils/request'

export default {
  data() {
    return {
      EnterpriseValue: '',
      Enterprise: [], // 企业
      ProductValue: '',
      Product: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1, // 后台获取页
      dialogFormVisible: false,
      dialogFormVisible3: false,
      form: {
        问题描述: '',
        计划完成时间: ''
      },
      form3: {
        问题描述: '',
        计划完成时间: ''
      },
      rules: {
        问题描述: [{ required: true, message: '请输入问题描述', trigger: 'blur' }],
        计划完成时间: [{ required: true, message: '请选择计划完成时间', trigger: 'change' }]
      },
      产品代码: '',
      bqliushuihao: '',
      产品代码3: '',
      bqliushuihao3: '',
      dialogVisible2: false,
      dialogImageUrl: '',
      loading2: false,
      tableData2: [],
      total2: 0, // 总条数
      pageSize2: 10, // 每页显示条数
      pageList2: 1, // 后台获取页
      loading3: false,
      tableData3: [],
      total3: 0, // 总条数
      pageSize3: 10, // 每页显示条数
      pageList3: 1, // 后台获取页
      action: '', // ↓
      upload: uploadFile3,
      fileList: [],
      dialogVisible: false,
      disabled: true, // 上传相关↑
      action3: '', // ↓
      upload3: uploadFile4,
      fileList3: [],
      disabled4: true // 上传相关↑
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id'
    ])
  },
  watch: {
    EnterpriseValue() {
      this.EnterpriseChange()
    }
  },
  created() {
    this.getEnterprise()
    this.searchTable2()
  },
  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 = []
      }
    },
    // 查询
    searchTable2() {
      this.loading3 = true
      this.tableData = []
      this.tableData2 = []
      this.tableData3 = []
      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.pageSize3, this.pageList3)
      ExecDatabase(Data).then(response => {
        if (response.data.rows.length !== 0) {
          response.data.rows.map(v => {
            this.tableData3.push({
              产品代码: v.产品代码,
              标签流水号: v.标签流水号,
              产品名称: v.产品名称,
              上传时间: v.上传时间
            })
          })
        }
        this.total3 = response.data.total
        this.loading3 = false
      })
    },
    // 查询
    searchTable(row) {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['标签流水号', row.标签流水号]
      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 => {
            if (v.图片保存名称 === null) {
              this.tableData.push({
                产品名称: v.产品名称,
                企业名称: v.企业名称,
                确认时间: v.确认时间,
                问题描述: v.问题描述,
                url: 0,
                审核流水号: v.审核流水号,
                审核图片流水号: v.审核图片流水号,
                是否通过: v.是否通过
              })
            } else {
              this.tableData.push({
                产品名称: v.产品名称,
                企业名称: v.企业名称,
                确认时间: v.确认时间,
                问题描述: v.问题描述,
                url: ImageUrl + v.图片保存名称 + '.' + v.suffix,
                审核流水号: v.审核流水号,
                审核图片流水号: v.审核图片流水号,
                是否通过: v.是否通过
              })
            }
          })
        }
        this.total = response.data.total
        this.loading = false
      })
    },
    // 图片预览
    showPicture(row) {
      this.dialogImageUrl = row.url
      this.dialogVisible2 = true
    },
    rowClick(row) {
      this.loading2 = true
      this.tableData2 = []
      var param = []
      param[0] = ['审核流水号', row.审核流水号]
      var Data = this.CreateData('11', '标签检测_已审核未处理_查询数据2', param, this.pageSize2, this.pageList2)
      ExecDatabase(Data).then(response => {
        if (response.data.rows.length !== 0) {
          response.data.rows.map(v => {
            if (v.图片保存名称 === null) {
              this.tableData2.push({
                产品名称: v.产品名称,
                企业名称: v.企业名称,
                确认时间: v.确认时间,
                问题描述: v.问题描述,
                url: 0,
                审核流水号: v.审核流水号,
                审核图片流水号: v.审核图片流水号,
                是否通过: v.是否通过
              })
            } else {
              this.tableData2.push({
                产品名称: v.产品名称,
                企业名称: v.企业名称,
                确认时间: v.确认时间,
                问题描述: v.问题描述,
                url: ImageUrl + v.图片保存名称 + '.' + v.suffix,
                审核流水号: v.审核流水号,
                审核图片流水号: v.审核图片流水号,
                是否通过: v.是否通过
              })
            }
          })
        }
        this.total2 = response.data.total
        this.loading2 = false
      })
    },
    // 反馈企业
    Enterprises(formName, row) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      if (this.$refs['upload3'] !== undefined) {
        this.$refs['upload3'].clearFiles()
      }
      this.form3.问题描述 = ''
      this.bqliushuihao3 = row.标签流水号
      this.dialogFormVisible3 = true
    },
    // 提交
    submitUpload3(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.action3 = ''
          this.action3 = `${uploadFile4}?&bqliushuihao=${this.bqliushuihao3}&questions=${this.form3.问题描述}`
          var param = []
          var Data = this.CreateData('11', '基础数据_企业信息_查询数据', param)
          ExecDatabase(Data).then(response => {
            this.$refs.upload3.submit()
            this.$message.success('保存成功')
            if (this.tableData3 && this.tableData3.length === 1 && this.pageList3 > 1) { // 判断删除的是否是最后一页最后一行，如果是且不是第一页，页数减一
              this.pageList3--
            }
            this.tableData = []
            this.tableData2 = []
            this.tableData3 = []
            this.searchTable2()
            this.dialogFormVisible3 = false
          })
        } else {
          return false
        }
      })
    },
    // 反馈专家
    editType(formName, row) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      if (this.$refs['upload'] !== undefined) {
        this.$refs['upload'].clearFiles()
      }
      this.form.问题描述 = ''
      this.form.计划完成时间 = ''
      this.产品代码 = row.产品代码
      this.bqliushuihao = row.标签流水号
      this.dialogFormVisible = true
    },
    // 提交
    submitUpload(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.action = ''
          this.action = `${uploadFile3}?&num=${this.产品代码}&bqliushuihao=${this.bqliushuihao}&questions=${this.form.问题描述}&endtime=${this.form.计划完成时间}`
          var param = []
          var Data = this.CreateData('11', '基础数据_企业信息_查询数据', param)
          ExecDatabase(Data).then(response => {
            this.$refs.upload.submit()
            this.$message.success('保存成功')
            if (this.tableData3 && this.tableData3.length === 1 && this.pageList3 > 1) { // 判断删除的是否是最后一页最后一行，如果是且不是第一页，页数减一
              this.pageList3--
            }
            this.tableData = []
            this.tableData2 = []
            this.tableData3 = []
            this.searchTable2()
            this.dialogFormVisible = false
          })
        } else {
          return false
        }
      })
    },
    adopt(row) {
      this.$confirm('此操作将通过该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['标签流水号', row.标签流水号]
        var Data = this.CreateData('12', '标签审核_反馈企业_编辑数据2', param)
        ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('处理成功')
            if (this.tableData && this.tableData.length === 1 && this.pageList > 1) { // 判断删除的是否是最后一页最后一行，如果是且不是第一页，页数减一
              this.pageList--
            }
            this.searchTable2()
          } else {
            this.$message.error('处理失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消处理'
        })
      })
    },
    // 上传前检测
    beforeUpload(file) {
      const isJPG = /^image\/.*/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传图片')
      }
      return isJPG
    },
    callOff() {
      this.dialogFormVisible = false
    },
    callOff3() {
      this.dialogFormVisible3 = false
    },
    // 上传
    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}文件上传失败，请检查上传文件是否正确！`)
      })
    },
    // 上传前检测
    beforeUpload3(file) {
      const isJPG = /^image\/.*/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传图片')
      }
      return isJPG
    },
    // 上传
    uploadSuccess3(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('提交成功')
      }
    },
    uploadFailure3(a, file, fileList) {
      fileList.map(file => {
        this.$message.error(`${file.name}文件上传失败，请检查上传文件是否正确！`)
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    },
    handleSizeChange2(val) {
      this.pageList2 = 1
      this.pageSize2 = val
      this.rowClick()
    },
    handleCurrentChange2(val) {
      this.pageList2 = val
      this.rowClick()
    },
    handleSizeChange3(val) {
      this.pageList3 = 1
      this.pageSize3 = val
      this.searchTable2()
    },
    handleCurrentChange3(val) {
      this.pageList3 = val
      this.searchTable2()
    }
  }
}
</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>
