<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="产品编码:">
              <el-input
                style="width: 90%"
                v-model="filterQuery.unionCode"
                placeholder="请输入产品编码/商品条码/医保编码" clearable
                @keyup.enter.native="keyupErp_submit($event)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品名称:">
              <el-input v-model="filterQuery.name" style="width: 90%" clearable placeholder="请输入产品名称"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="规格型号:">
              <el-input
                style="width: 90%"
                v-model="filterQuery.spec" clearable
                placeholder="请输入规格型号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="配送企业:">
              <el-select
                v-model="filterQuery.supId"
                filterable
                style="width: 90%"
                remote
                clearable="true"
                reserve-keyword
                placeholder="请输入配送企业"
                :remote-method="findMethod"
                size="mini"
                :loading="loading"
              >
                <el-option
                  v-for="item in fromOptions"
                  :key="item.name"
                  :label="item.name"
                  :value="item.erpId"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="审核状态:">
              <el-select v-model="filterQuery.checkStatus" style="width: 90%" placeholder="请选择审核状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="已审核" value="2"></el-option>
                <el-option label="未审核" value="1"></el-option>
                <el-option label="未通过" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


        <el-col :span="6">
        <el-form-item label="供应商:">
          <el-select
            v-model="filterQuery.companyName"
            filterable
            style="width: 90%"
            remote
            clearable="true"
            reserve-keyword
            placeholder="请选择供应商"
            :remote-method="findCompany"
            :loading="loading"
          >
            <el-option
              v-for="item in companyNames"
              :key="item.companyName"
              :label="item.companyName"
              :value="item.companyName"
            >
              <span style="float: left">{{ item.companyName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="外部系统:">
          <el-select v-model="filterQuery.thirdSys"   style="width: 90%" placeholder="请选择第三方系统" @change="thirdSysChange">
            <el-option
              v-for="item in thirdSys"
              :key="item.value"
              :label="item.thirdName"
              :value="item.thirdId"
            >
              <span style="float: left">{{ item.thirdName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="80"></el-table-column>
        <el-table-column label="产品编码" prop="code"></el-table-column>
        <el-table-column label="产品名称" prop="name" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="spec" show-overflow-tooltip></el-table-column>
        <el-table-column label="计量单位" prop="measname" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案凭证" prop="registerNo" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产厂家" prop="manufactory" show-overflow-tooltip></el-table-column>
        <el-table-column label="配送企业" prop="corpName" show-overflow-tooltip></el-table-column>

        <el-table-column label="审核状态" prop="checkStatus" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ checkMap[scope.row.checkStatus] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核意见" prop="remark" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="detailDialog(scope.row)"
            >详情
            </el-button>
            <el-button type="text" size="small" @click.native.stop="deleteDialog(scope.row)"
                        :disabled="scope.row.checkStatus==2"
                       v-if="thirdSysDetail.fromType!=0"
            >删除
            </el-button>

            <el-button type="text" size="small"
                       :disabled="scope.row.checkStatus==2"
                       @click="checDialog(scope.row)"
            >审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="handleCurrentChange"
      ></pagination>

    </el-card>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="产品信息详情"
      :visible.sync="thrProductsDetailVisible"
      width="80%"
      v-if="thrProductsDetailVisible"
    >
      <thrProductsDetail
        :data="thisData"
      ></thrProductsDetail>
    </el-dialog>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="审核"
      :visible.sync="checkVisible"
      width="45%"
      v-if="checkVisible"
    >
      <el-form>
        <el-form-item>
          审核意见：
          <el-input type="text" v-model="remark" style="width: 50%" placeholder="请输入审核意见"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="checkVisible1">取消</el-button>
          <el-button type="primary" @click="checkPass(2)">通过</el-button>
          <el-button type="primary" @click="checkPass(3)">不通过</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getThrProducts, delThrProducts, updateProduct } from '@/api/thrsys/thrProductsAdd'
import { getBasicThirdSys, filterDetailByKey } from '@/api/basic/basicThirdSys'
import { selectIp } from '@/api/param/systemParamConfig'
import thrProductsDetail from './thrProductsDetail'
import ThrProductsAdd from '@/views/thirdSys/product/thrProductsAdd'
import { getBasicUnitMaintains2 } from '@/api/basic/basicUnitMaintain'
import { filterCustomers } from '@/api/auth/customer'

export default {
  data() {
    return {
      showSearch: true,
      filterQuery: {
        code: null,
        name: '',
        spec: null,
        registerNo: null,
        manufactory: null,
        thirdSys: '',
        companyName: null,
        supId: null,
        page: 1,
        limit: 20
      },
      total: 0,
      thirdSys: [],
      thirdSysDetail: null,
      list: [],
      uploadFileUrl: null,
      fileList: [],
      uploadData: {
        thirdSys: 'thirdId'
      },
      checkVisible: false,
      newType: 1,
      thrProductsDetailVisible: false,
      thisData: null,
      loading: false,
      checkMap: {
        1: '未审核',
        2: '已审核',
        3: '未通过'
      },
      fromOptions: [],
      companyNames: [],
      curRow: null,
      remark: null
    }
  },

  methods: {
    onReset() {
      this.$router.push({
        path: ''
      })
      this.filterQuery = {
        code: null,
        spec: null,
        registerNo: null,
        manufactory: null,
        name: '',
        companyName: null,
        thirdSys: this.thirdSys[0].thirdId,
        page: 1,
        limit: 20
      }
      this.getList()
    },
    checDialog(row) {
      this.remark = null
      this.checkVisible = true
      this.curRow = row
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    search() {
      this.filterQuery.page = 1
      this.getList()
    },
    checkVisible1() {
      this.checkVisible = false
      this.remark = ''
    },

    getList() {
      if (this.filterQuery.thirdSys == null) {
        this.$message.warning('请先选择第三方系统！')
        return
      }
      this.loading = true
      this.filterQuery.thirdSysFk=this.filterQuery.thirdSys;
      getThrProducts(this.filterQuery)
        .then((response) => {
          // this.loading = false;
          if (response.code == 20000) {
            this.list = response.data.list || []
            this.total = response.data.total || 0
          } else {
            this.$message.error(response.message)
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
          this.list = []
          this.total = 0
        })
    },
    deleteDialog(row) {
      this.$confirm('此操作将永久删除该产品信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let dQuery = {
            id: row.id
          }
          delThrProducts(dQuery)
            .then((response) => {
              this.loading = false
              if (response.code == 20000) {
                this.$message.success('删除成功')
                this.getList()
              } else {
                this.$message.success(response.message)
              }
            })
            .catch(() => {
              this.loading = false
            })

        })
        .catch(() => {
        })
    },

    detailDialog(row) {
      this.thisData = row
      this.thrProductsDetailVisible = true
    },
    getBasicThirdSys() {
      let query = {
        enabled: true
      }
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || []
          this.filterQuery.thirdSys = this.thirdSys[0].thirdId
          this.uploadData.thirdSys = this.filterQuery.thirdSys
          this.getThirdSysDetail()
          this.selectSysParam()
          this.getList()
        })
        .catch(() => {
          this.loading = false
          this.list = []
        })
    },
    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys,
        key: 'piQueryUrl'
      }
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data
        })
        .catch(() => {
          this.loading = false
          this.list = []
        })

    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page
      this.getList()
    },
    handleChange(response, files, fileList) {
      if (response.code != 20000) {
        this.$message.error(response.message)
      } else {
        this.$message.success('文件上传成功,请稍后刷新查看！')
      }
    },
    selectSysParam() {
      let query = {
        key: 'thirdIpUrl',
        thirdSysFk: this.filterQuery.thirdSys
      }
      selectIp(query).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.thridUrl + '/udiwms/erp/pi/upload'
        }
      })
    },
    thirdSysChange() {
      this.uploadData.thirdSys = this.filterQuery.thirdSys
      this.getThirdSysDetail()
    },
    closeAddDialog() {
      this.addProductVisible = false
      Object.keys(this.newProductData).forEach(key => this.newProductData[key] = null)
    },
    checkPass(val) {
      let query = {
        id: this.curRow.id,
        checkStatus: val,
        remark: this.remark
      }
      updateProduct(query).then((res) => {
        if (res.code == 20000) {
          this.getList()
          this.checkVisible = false
          this.$message.success('操作成功')
        } else {
          this.$message.error(res.message)
        }
      }).catch((error) => {
        this.$message.error(error.message)
      })
    },
    findMethod(query) {
      this.fromOptions = []
      let cQuery = {
        key: query,
        customerName: query
      }
      getBasicUnitMaintains2(cQuery).then((res) => {
        this.fromOptions = res.data.list || []
      }).catch(() => {
        this.fromOptions = []
      })
    },
    findCompany(query) {
      this.companyNames = []
      let cQuery = {
        key: query
      }
      filterCustomers(cQuery).then((res) => {
        this.companyNames = res.data.list || []
      }).catch((error) => {
        this.companyNames = []
      })
    }
  },
  components: {
    thrProductsDetail, ThrProductsAdd
  },
  mounted() {
  },
  created() {
    this.getBasicThirdSys()
    this.findMethod()
  }
}
</script>
