<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      title="选择采购申请"
      :visible.sync="showTroubleshooting"
      width="70%"
      :close-on-press-escape="false"
      :before-close="handleClose"
      top="10vh"
    >
      <template>
        <div class="bbsl-table-search">
          <el-form v-model="queryParam" :inline="true">
            <el-form-item label="申请单位">
              <el-input
                size="small"
                v-model="querys[0].value"
                clearable
                placeholder="请输入申请单位"
              ></el-input>
            </el-form-item>
            <el-form-item label="制单日期">
              <el-date-picker
                v-model="querys[1].value"
                type="daterange"
                size="small"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                size="small"
                type="danger"
                icon="el-icon-search"
                @click="handleSearch"
                >搜索</el-button
              >
              <el-button
                size="small"
                icon="el-icon-refresh"
                @click="handleReset"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <el-table
          ref="multipleSelection"
          v-loading="loading"
          border
          size="mini"
          :data="tableData"
          height="400px"
          :row-key="rowKey"
          :style="{width: '100%', height: '500px'}"
          :max-height="tableMaxHeight"
          :cell-style="{'text-align': 'center'}"
          :header-cell-style="{'text-align': 'center'}"
          @selection-change="handleSelectionChange"
          @select="tableSelect"
        >
          <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
          />
          <el-table-column type="index" width="55" label="序号" />
          <el-table-column
            prop="applicationNum"
            min-width="13%"
            label="申请编号"
            show-overflow-tooltip
            align="center"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="preparedOrgName"
            min-width="10%"
            show-overflow-tooltip
            label="申请单位"
            align="center"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="preparedUserName"
            min-width="10%"
            label="申请人"
            align="center"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="preparedDate"
            show-overflow-tooltip
            min-width="10%"
            align="center"
            label="制单日期"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="auditingUser"
            min-width="10%"
            label="审核人"
            align="center"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="auditingDate"
            min-width="10%"
            label="审核日期"
            show-overflow-tooltip
            align="center"
            resizable
          >
          </el-table-column>
          <!-- <el-table-column
            prop="applicationDate"
            show-overflow-tooltip
            min-width="10%"
            align="center"
            label="需求日期"
            resizable
          >
          </el-table-column> -->
          <el-table-column
            prop="procurementMethod"
            show-overflow-tooltip
            min-width="10%"
            align="center"
            label="采购类型"
            resizable
          >
            <template>
              谈判采购（多轮）
            </template>
          </el-table-column>
          <el-table-column
            prop="isAgencyProcure"
            show-overflow-tooltip
            min-width="10%"
            align="center"
            v-if="getMethodauth('daicai')"
            label="是否代采"
            resizable
          >
            <template v-slot="{row}">
              {{ getDictName('cgsq-sfdc', row.isAgencyProcure) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="pathName"
            show-overflow-tooltip
            min-width="10%"
            align="center"
            label="查看明细"
            resizable
          >
            <template slot-scope="scope">
              <div>
                <el-button
                  @click="handleDetails(scope.row)"
                  size="small"
                  type="primary"
                  >查看明细</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div style="float: right; margin-top: 10px">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="申请明细"
      :visible.sync="dialogVisible"
      width="80%"
      top="7vh"
      class="fullDialog"
      :before-close="dialogBeforeClose"
    >
      <div style="height: 100%">
        <el-table
          :border="true"
          stripe
          fit
          size="mini"
          ref="multipleSelection"
          :data="data"
        >
          <el-table-column
            type="index"
            show-overflow-tooltip
            min-width="50"
            align="center"
            label="序号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matCode"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="企业物料编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matPlatcode"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="平台物料编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matName"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="物料名称"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matMaterial"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="物料牌号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matSpec"
            show-overflow-tooltip
            min-width="110px"
            align="center"
            label="规格型号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matNum"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="数量"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="planCode"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="计划编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matUnit"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="计量单位"
            resizable
          >
          </el-table-column>
          
          <el-table-column
            prop="demandDate"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="需求日期"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matBrand"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="品牌"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matBaseExt"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="基础扩展"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matOtherExt"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="其他扩展"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxRate"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="税率(%)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matIfPlat"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="是否集采"
            resizable
          >
            <template v-slot="{row}">
              {{ getDictName('cglx-ywlx', row.matIfPlat) }}
            </template>
          </el-table-column>
          
          <el-table-column
            prop="unloadAddress"
            show-overflow-tooltip
            min-width="130px"
            align="center"
            label="送货地址"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="remarks"
            show-overflow-tooltip
            min-width="130px"
            align="center"
            label="企业补充说明"
            resizable
          >
          </el-table-column>
        </el-table>
      </div>
      <div class="common-pagenation">
        <el-pagination
          ref="paginationPanel"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="params.pageBean.page"
          :page-sizes="[20, 30, 40, 50]"
          :page-size="params.pageBean.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="params.pageBean.total"
        >
        </el-pagination>
      </div>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import basicData from '@/api/basicData'
import utils from '@/utils.js'
export default {
  name: 'CmgtSaftyDangerCheckplanFormDialogTroubleshooting', // 排查计划明细
  props: {
    useOrgList: Array,
  },
  data() {
    return {
      showTroubleshooting: false, // 控制弹层显示与隐藏
      multipleSelection: [],
      selectedValue: [],
      dialogVisible: false,
      tableHeight: 600,
      loading: false,
      data: [],
      matIfPlat: '',
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'preparedOrgName',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'BETWEEN',
          parentGroup: '',
          property: 'preparedDate',
          relation: 'AND',
          value: '',
        },
        {
          group: 'groups',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: '3',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'procurementMethod',
          relation: 'AND',
          value: '6',
        },
        {
          group: 'mains',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'preparedOrgCode',
          relation: 'AND',
          value: '',
        },
        {
          group: 'mains',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'agencyProcureOrgCode',
          relation: 'OR',
          value: '',
        },
        {
          group: 'groups',
          operation: 'IN',
          parentGroup: '',
          property: 'id',
          relation: 'OR',
          value: '',
        }
      ],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        querys: [],
      },
      currentUser:{},
    }
  },
  mixins: [TableMixin],
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
  },
  methods: {
    // 动态计算表格的高度，自适应当前容器
    calcTableHeight() {
      this.tableHeight = this.$refs.tableContent.offsetHeight
    },
    rowKey(row) {
      return row.id
    },
    //搜索
    handleSearch() {
      this.loadData()
    },
    //重置
    handleReset() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = '3'
      this.querys[3].value = '6'
      this.querys[4].value = this.currentUser.org[0].orgCode
      this.querys[5].value = this.currentUser.org[0].orgCode
      this.querys[6].value = this.useOrgList
      this.loadData()
    },
    //显示弹框
    add(row) {
      this.matIfPlat = row
      this.showTroubleshooting = true
      this.$nextTick(function () {
        this.useOrgList.forEach((item) => {
          if (this.tableData.some((data) => data.id === item.id)) {
            this.$refs.multipleSelection.toggleRowSelection(item, true) //这样就可以了
          } else {
            this.$refs.multipleSelection.toggleRowSelection(item, true) //这样就可以了
          }
        })
      })
      let ids = this.useOrgList.map(item=>{
        return item.id
      })
      console.log(this.useOrgList);
      this.querys[4].value = this.currentUser.org[0].orgCode
      this.querys[5].value = this.currentUser.org[0].orgCode
      this.querys[6].value = ids
      this.loadData()
    },
    // 获取list
    loadData() {
      this.loading = true
      this.tableData = []
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      if (this.querys[3].value) {
        this.queryParam.querys.push(this.querys[3])
      }
      if (this.querys[4].value) {
        this.queryParam.querys.push(this.querys[4])
      }
      if (this.querys[5].value) {
        this.queryParam.querys.push(this.querys[5])
      }
      if (this.querys[6].value) {
        this.queryParam.querys.push(this.querys[6])
      }
      this.queryParam.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'matIfPlat',
        relation: 'AND',
        value: this.matIfPlat,
      })
      this.queryParam.sorter = [
        {
          direction: 'DESC',
          property: 'createTime',
        },
      ]
      this.$http
        .post(
          '${portal}/purchasingApplication/v1/pageCurOrgData',
          this.queryParam
        )
        .then((res) => {
          if (res.data.state) {
            this.queryParam.pageBean.total = res.data.value.total
            this.tableData = res.data.value.rows
            this.$set(this.tableData)
            this.loading = false
          }
        })
    },
    tableSelect(rows, row) {
      let selected = rows.length && rows.indexOf(row) !== -1
      if (!selected) {
        this.selectedValue = rows
      } else {
        this.selectedValue = []
      }
    },
    // 查看明细
    handleDetails(row) {
      console.log(row, 'row')
      this.params.querys = []
      this.params.querys.push(
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'purchasingApplicationId',
          relation: 'AND',
          value: row.id,
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matIfPlat',
          relation: 'AND',
          value: row.matIfPlat,
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: '1',
        }
      )
      this.getDetals()
      this.dialogVisible = true
    },
    getDetals() {
      this.$http
        .post('${portal}/bizPurchasingApplicationDetails/v1/page', this.params)
        .then((res) => {
          console.log(res, 'res')
          if (res.data.state) {
            this.data = res.data.value.rows
            this.params.pageBean.total = res.data.value.total
            this.params.pageBean.page = res.data.value.page
            this.params.pageBean.pageSize = res.data.value.pageSize
          }
        })
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击关闭按钮
    handleClose() {
      this.$refs.multipleSelection.clearSelection()
      this.showTroubleshooting = false
    },
    // 保存按钮
    handleSave() {
      let list = []
      list = this.multipleSelection
      console.log(list, 'list')
      this.$emit('addDetail', list)
      this.handleClose()
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData()
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData()
    },
    currentChange(page) {
      this.params.pageBean = {...this.params.pageBean, page}
      this.getDetals()
    },
    sizeChange(pageSize) {
      this.params.pageBean = {...this.params.pageBean, pageSize}
      this.getDetals()
    },
    getMethodauth(elAuth) {
      let allMethod = window.sessionStorage.allMethod
      let methodAuth = window.sessionStorage.methodAuth
      let allMethodArray = allMethod && allMethod.split(',')
      let methodAuthArray = methodAuth && methodAuth.split(',')
      // 无权限则隐藏按钮
      if (
        methodAuthArray &&
        methodAuthArray.includes(elAuth) &&
        allMethodArray &&
        allMethodArray.includes(elAuth)
      ) {
        return true
      } else {
        return false
      }
    },
    // 获取字典值翻译
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
  },
}
</script>

<style scoped lang="scss">
.dialog-footer {
  width: 100%;
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
.fullDialog {
  height: 100%;
  /deep/.el-dialog__body {
    height: 600px;
  }
  /deep/.el-table {
    height: 100%;
    overflow-y: scroll;
  }
}
</style>
