<template>
  <div class="purchase-order-base-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item prop="order_id">
            <el-input
              v-model.trim="queryForm.order_id"
              clearable
              :placeholder="__('订单编号')"
              style="width: 120px"
            />
          </el-form-item>
          <el-form-item prop="purchase_order_title">
            <el-input
              v-model.trim="queryForm.purchase_order_title"
              clearable
              :placeholder="__('货品名称')"
              style="width: 120px"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.purchase_order_approval_state"
              clearable
              :placeholder="__('审核状态')"
              style="width: 120px"
            >
              <el-option
                v-for="item in options"
                :key="item.purchase_order_approval_state"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.purchase_order_state"
              clearable
              :placeholder="__('采购单状态')"
              style="width: 120px"
            >
              <el-option
                v-for="item in orderOptions"
                :key="item.purchase_order_state"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <tree-select
              ref="treeSelect"
              :checked-keys="defaultCheckedKeys"
              clearable
              :data="data"
              :default-props="defaultProps"
              :height="300"
              :node-key="nodeKey"
              size="small"
              style="width: 120px"
              :width="220"
              @change="change"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="100"
      >
        <template #default="{ row }">
          <el-button :disabled="row.purchase_order_approval_state == 1" type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button :disabled="row.purchase_order_approval_state != 1" type="text" @click="showSupplyTable(row)">
            {{ __('报价单') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('采购编号')"
        prop="purchase_order_id"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('采购状态')"
        prop="purchase_order_state"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag :type='({"1000":"warning","1010":"success","1020":"info","1030":"error","1040":"warning"})[row.purchase_order_state]'>
            {{ ({"1000":"匹配中","1010":"已完成","1020":"已取消","1030":"被驳回","1040":"审核中"})[row.purchase_order_state] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('审核状态')"
        prop="purchase_order_approval_state"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag :type='({"0":"warning","1":"success","2":"danger"})[row.purchase_order_approval_state]'>
            {{ ({"0":"待审核","1":"审核通过","2":"审核驳回"})[row.purchase_order_approval_state] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('货品名称')"
        prop="purchase_order_title"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('下单时间')"
        prop="purchase_order_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('货品描述')"
        prop="purchase_order_description"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('货品分类')"
        prop="category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类名称')"
        prop="purchase_category_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('货品图片')"
        prop="purchase_product_images"
        show-overflow-tooltip
        width="100px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.purchase_product_images"
            :preview-src-list="[scope.row.purchase_product_images]"
            :src="scope.row.purchase_product_images"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('规格')"
        prop="purchase_product_spec"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('采购数量')"
        prop="purchase_order_quantity"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('数量单位')"
        prop="purchase_order_unit"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('期望单价上限')"
        prop="purchase_origin_price_max"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('买家编号')"
        prop="buyer_user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('买家姓名')"
        prop="buyer_user_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('供应商编号')"
        prop="supply_user_id"
        show-overflow-tooltip
        width="120px"
      />
      <el-table-column
        align="center"
        :label="__('供应商名称')"
        prop="supply_user_name"
        show-overflow-tooltip
        width="120px"
      />
      <el-table-column
        align="center"
        :label="__('期望产地')"
        prop="purchase_city"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatDateTwo"
        :label="__('期望收货日期')"
        prop="purchase_origin_receive_time"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatDateOne"
        :label="__('截止时间')"
        prop="purchase_deadline"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatInvoice"
        :label="__('是否开票')"
        prop="purchase_invoice_enable"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('平台佣金')"
        prop="purchase_commission_fee"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <supply ref="supply" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/purchase/order/base'
  import Edit from './components/PurchaseOrderBaseEdit'
  import Supply from './components/SupplyOrderBaseTable'
  import {renderDate} from '@/utils/date'
  import TreeSelect from '@/components/treeSelect'
  import {getTree} from "@/api/base/product/category";

  export default {
    name: 'PurchaseOrderBase',
    components: { Edit, Supply, TreeSelect },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        options: [
          {
            value: '0',
            label: this.__('待审核'),
          },
          {
            value: '1',
            label: this.__('审核通过'),
          },
          {
            value: '2',
            label: this.__('审核失败'),
          },
        ],
        orderOptions: [
          {
            value: 1000,
            label: this.__('匹配中'),
          },
          {
            value: 1010,
            label: this.__('已完成'),
          },
          {
            value: 1020,
            label: this.__('已取消'),
          },
          {
            value: 1030,
            label: this.__('被驳回'),
          },
          {
            value: 1040,
            label: this.__('审核中'),
          },
        ],
        value: '',
        data: [], // 产品规格数据
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        nodeKey: 'id',
        defaultCheckedKeys: [],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
      this.getTree()
    },
    methods: {
      __,
      formatInvoice(row) {
        return row.purchase_invoice_enable ? '是' : '否'
      },
      formatDateOne(row) {
        return renderDate(row.purchase_deadline)
      },
      formatDateTwo(row) {
        return renderDate(row.purchase_origin_receive_time)
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      showSupplyTable(row) {
        this.$refs['supply'].showTable(row)
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      // 获取规格数据
      async getTree() {
        const { data } = await getTree()
        this.data = data
      },
      change(id) {
        this.queryForm.category_id = id
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
