<template>
  <div class="app-container new-box">
    <el-card>
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <div class="filter-box">
          <div class="filter-lf">
            <el-form-item label="供应商名称" prop="supplierCompanyId">
              <el-select v-model="queryParams.supplierCompanyId" placeholder="请选择" clearable size="small">
                <el-option v-for="dict in spList" :key="dict.id" :label="dict.companyAme" :value="dict.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="类别" prop="purchasingCategoriesId">
              <el-select v-model="queryParams.purchasingCategoriesId" placeholder="请选择" clearable size="small">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.BS_MATERIAL_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
        </div>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-dropdown trigger="click" v-hasPermi="['bs:quotation-sheet:create']" @command="handleCommand">
            <el-button type="primary" size="mini" style="margin-right:5px;">
              <i class="el-icon-plus"></i> 创建报价单
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="beforeHandleCommand(1)">
                <el-button type="text">内部报价</el-button>
              </el-dropdown-item>
              <el-dropdown-item :command="beforeHandleCommand(2)">
                <el-button type="text">外部报价</el-button>
              </el-dropdown-item>

            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading" v-hasPermi="['bs:expense-apply:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <el-table stripe border v-loading="loading" :data="list">
        <el-table-column label="报价编号" align="center" prop="number" />
        <el-table-column label="供应商名称" align="center" prop="supplierNameList" />
        <el-table-column label="类别" align="center" prop="purchasingCategoriesId">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_MATERIAL_TYPE" :value="scope.row.purchasingCategoriesId" />
          </template>
        </el-table-column>
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="报价截止时间" align="center" prop="cutoffTime" width="250">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.cutoffTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采购名称" align="center" prop="projectName" />
        <el-table-column label="金额" align="center" prop="money">
          <template v-slot="scope">
            {{(scope.row.money || scope.row.money == 0) ? scope.row.money : '-'}} 元
          </template>
        </el-table-column>
        <el-table-column label="发布状态" align="center" prop="status">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_EXPENSE_APPLY_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="报价状态" align="center" prop="quotationStatus">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_QUOPRICE_TYPE" :value="scope.row.quotationStatus" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)" v-hasPermi="['bs:quotation-sheet:show']">查看报价</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" :disabled="scope.row.status!=0" v-hasPermi="['bs:quotation-sheet:update']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['bs:quotation-sheet:detail']">详情</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" :disabled="scope.row.status!=0" v-hasPermi="['bs:quotation-sheet:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 查看报价 -->
    <ShowPriceDialog :quotationId="quotationId" :isOpen="priceShow" @onCancel="priceShow=false" @onConfirm="priceShow=false" />
  </div>
</template>

<script>
import { createQuotationSheet, updateQuotationSheet, deleteQuotationSheet, getQuotationSheet, getQuotationSheetPage, exportQuotationSheetExcel } from "@/api/bs/quotationSheet";
import Editor from '@/components/Editor';
import { listData } from "@/api/system/dict/data";
import { getSupplierCompanyPage } from "@/api/procurement/supplierMsg";
export default {
  name: "QuotationSheet",
  components: {
    Editor,
    ShowPriceDialog: () => import('./components/showPriceDialog'),
  },
  data() {
    return {
      quotationId: null,
      spList: [], //供应商名称列表
      priceShow: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报价单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        purchasingCategoriesId: null,
        pageNo: 1,
        pageSize: 10,
        supplierCompanyId: null,
        remark: null,
        files: null,
        createTime: [],
        companyName: [],
        number: null,
        type: null,
        productName: null,
        cutoffTime: [],
        projectId: null,
        projectName: null,
        releaseStatus: null,
        quotationStatus: null,
        status: null,
        processInstanceId: null,
        auditProgress: null,
        procurementSchedule: null,
        productQuantity: null,
        purchasingCategoriesId: null,
        beginTime: [],
        endTime: [],
        sectionId: null,
        deliveryAddress: null,
        deliverer: null,
        deliveryPhone: null,
        purchaseContent: null,
        applyTime: [],
        applicant: null,
        materialIdList: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.handleInitList()
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    handleShow(obj) {
      this.quotationId = obj.id
      this.priceShow = true
    },
    handleDetail(row) {
      this.$router.push({ path: "/bs/quotationSheet/in", query: { id: row.id, flag: 'show', title: '报价单详情', typeStr: 'in' } });
    },
    handleCommand(val) {
      const { index, row } = val
      //  1 内部 2外部
      if (index === 1) {
        this.$router.push({ path: "/bs/quotationSheet/in", query: { id: null, flag: 'add', title: '报价单添加', typeStr: 'in' } });
      } else {
        this.$router.push({ path: "/bs/quotationSheet/out", query: { id: null, flag: 'add', title: '报价单添加', typeStr: 'out' } });
      }
    },
    beforeHandleCommand(index, data) {
      return {
        'index': index,
        'row': data
      }
    },
    handleInitList() {
      getSupplierCompanyPage({
        pageNo: 1,
        pageSize: 1000,
      }).then((res) => {
        this.spList = res.data.list || []
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getQuotationSheetPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        remark: undefined,
        files: undefined,
        number: undefined,
        type: undefined,
        productName: undefined,
        cutoffTime: undefined,
        projectId: undefined,
        projectName: undefined,
        releaseStatus: undefined,
        quotationStatus: undefined,
        status: undefined,
        processInstanceId: undefined,
        auditProgress: undefined,
        procurementSchedule: undefined,
        productQuantity: undefined,
        purchasingCategoriesId: undefined,
        beginTime: undefined,
        endTime: undefined,
        sectionId: undefined,
        deliveryAddress: undefined,
        deliverer: undefined,
        deliveryPhone: undefined,
        purchaseContent: undefined,
        applyTime: undefined,
        applicant: undefined,
        materialIdList: undefined,
        deptId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(121313123);
      this.$router.push({ path: "/bs/quotationSheet/in", query: { id: row.id, flag: 'edit', title: '报价单', typeStr: 'in' } });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateQuotationSheet(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createQuotationSheet(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除报价单编号为"' + id + '"的数据项?').then(function () {
        return deleteQuotationSheet(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有报价单数据项?').then(() => {
        this.exportLoading = true;
        return exportQuotationSheetExcel(params);
      }).then(response => {
        this.$download.excel(response, '报价单.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>

<style lang='scss' scoped>
@import '@/assets/styles/header/index.scss';
@import '@/assets/styles/common/index.scss';
</style>
