<template>
  <div class="list-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form ref="queryForm" :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item prop="contractCode">
            <el-input v-model="queryForm.contractCode" placeholder="合同编号" @keyup.enter.native="restFetchData" />
          </el-form-item>
          <el-form-item prop="custName">
            <el-input v-model="queryForm.custName" placeholder="客户名称" @keyup.enter.native="restFetchData" />
          </el-form-item>
          <el-form-item prop="orderType">
            <el-select
              v-model="queryForm.orderType"
              clearable
              placeholder="交付类型"
              style="width: 100%"
              @keyup.enter.native="restFetchData">
              <el-option label="软件" value="10" />
              <el-option label="硬件" value="20" />
            </el-select>
          </el-form-item>
          <el-form-item prop="projectManName">
            <el-input v-model="queryForm.projectManName" placeholder="项目经理" @keyup.enter.native="restFetchData" />
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="restFetchData">查询</el-button>
            <el-button icon="el-icon-refresh-right" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :span="12" />
      <vab-query-form-right-panel :span="12">
        <table-tool :columns="columns" :show-columns.sync="showColumns" table-type="deliverWorkOrderTable" />
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table ref="table" v-loading="listLoading" border :data="list" :height="$baseTableHeight(2)">
      <el-table-column align="center" label="序号" show-overflow-tooltip width="80">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in showColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable"
        :width="item.width">
        <template #default="{ row }">
          <el-button v-if="item.prop === 'orderCode'" style="font-size: 14px" type="text" @click="handleDetail(row)">
            {{ row.orderCode }}
          </el-button>
          <span v-else-if="item.prop === 'orderType'">
            {{ row.orderType == '10' ? '软件' : '硬件' }}
          </span>
          <span v-else-if="item.prop === 'product'">
            {{ selectDictLabel(productLineOptions, row.product) }}
          </span>
          <span v-else-if="item.prop === 'orderStatus'">
            {{ selectDictLabel(deliveryStatusOptions, row.orderStatus) }}
          </span>
          <span v-else-if="item.prop === 'createdTime'">
            {{ parseTime(row.createdTime, '{y}-{m}-{d}') }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="90">
        <template #default="{ row }">
          <el-button type="text" @click="handleUpdateUser(row)">成员</el-button>
          <el-button v-if="row.orderStatus != 20" type="text" @click="handleFinish(row)">完成</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image class="vab-data-empty" :src="require('@/assets/empty_images/data_empty.png')" />
      </template>
    </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" /> -->
    <!--反馈-->
    <finish ref="finish" @fetch-data="restFetchData" />
    <!-- 更新项目经理交付经理 -->
    <update-user ref="updateUser" @fetch-data="restFetchData" />
  </div>
</template>

<script>
  import to from 'await-to-js'
  import deliverApi from '@/api/work/deliver'
  import TableTool from '@/components/table/TableTool'
  import Finish from './components/finish'
  import UpdateUser from './components/updateUser'

  export default {
    name: 'WorkOrder',
    components: { TableTool, Finish, UpdateUser },
    data() {
      return {
        activeName: 'first',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          contractCode: '',
          custName: '',
          orderType: '',
          projectManName: '',
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        listLoading: false,
        list: [],
        selectRows: [],
        showColumns: [],
        columns: [
          {
            label: '交付工单号',
            width: '160px',
            prop: 'orderCode',
          },
          {
            label: '关联合同',
            width: 'auto',
            prop: 'contractCode',
          },
          {
            label: '交付状态',
            width: '160px',
            prop: 'orderStatus',
            disableCheck: true,
          },
          {
            label: '交付类型',
            width: '160px',
            prop: 'orderType',
          },
          {
            label: '关联客户',
            width: 'auto',
            prop: 'custName',
          },
          {
            label: '关联项目',
            width: 'auto',
            prop: 'projectName',
          },
          {
            label: '项目经理',
            width: '160px',
            prop: 'projectManName',
          },
          {
            label: '交付经理',
            width: '160px',
            prop: 'deliverManName',
          },
          {
            label: '产品线',
            width: 'auto',
            prop: 'product',
          },
          {
            label: '完成信息',
            width: '160px',
            prop: 'finishRemark',
          },
          {
            label: '创建时间',
            width: 'auto',
            prop: 'createdTime',
          },
        ],
        deliveryStatusOptions: [],
        productLineOptions: [],
      }
    },
    watch: {
      showColumns: function () {
        this.$nextTick(() => this.$refs.table.doLayout())
      },
    },
    activated() {
      this.fetchData()
    },
    mounted() {
      this.getOptions()
      this.fetchData()
      // deliverApi.addDeliverOrder({ contractId: 47 })
    },

    methods: {
      getOptions() {
        Promise.all([this.getDicts('delivery_status'), this.getDicts('sys_product_line')])
          .then(([deliveryStatus, productLine]) => {
            this.deliveryStatusOptions = deliveryStatus.data.values || []
            this.productLineOptions = productLine.data.values || []
          })
          .catch((err) => console.log(err))
      },
      restFetchData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = { ...this.queryForm }
        const [err, res] = await to(deliverApi.getDeliverOrderList({ ...params }))
        this.listLoading = false
        if (err) return (this.listLoading = false)
        if (res.code == 200 && res.data) {
          this.list = res.data.list || []
          this.total = res.data.total
        }
        this.$nextTick(() => this.$refs.table.doLayout())
      },
      handleFinish(row) {
        this.$refs['finish'].showEdit(row)
      },
      handleUpdateUser(row) {
        this.$refs['updateUser'].open(row)
      },
      //详情
      handleDetail(row) {
        this.$router.push({
          name: 'DeliveryPlan',
          query: {
            id: row.id,
          },
        })
      },
      reset() {
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          name: '',
          orderTypeName: '',
          orderStatus: '',
          assignUserName: '',
        }
        this.fetchData()
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
    },
  }
</script>

<style lang="scss" scoped>
  $base: '.list';
</style>
