<!--
@name: 中转策略
@author: 张泽贤
@time: 2022-07-29
@doc: 
-->
<template>
  <div class="pages">
    <p-spin :spinning="spinning" v-if="!editType">
      <p-card style="margin-bottom: 10px">
        <p-form-model ref="formTarget" :model="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" class="plg-grid-from">
          <a-row>
            <a-col :span="6">
              <p-form-model-item label="策略名称">
                <p-input v-model="form.name" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="状态">
                <p-select v-model="form.status" :options="options.GENERAL_STATUS" allowClear />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item :wrapper-col="{ span: 24 }" style="text-align: right">
                <a-space>
                  <p-button type="primary" @clickSync="onSearch" icon="search">查询</p-button>
                  <p-button @click="onReset">重置</p-button>
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>
        </p-form-model>
      </p-card>
      <p-card>
        <a-space slot="extra">
          <p-button type="primary" @click="toEdit('add')" icon="plus">新增</p-button>
          <p-button type="primary" @click="onStatus(1)" :disabled="!selectedRowKeys.length">启用</p-button>
          <p-button type="danger" @click="onStatus(0)" :disabled="!selectedRowKeys.length">停用</p-button>
        </a-space>
        <p-table
          ref="masterTable"
          :id="$route.name + '_mainTable'"
          dragle
          bordered
          row-key="id"
          hasRowSelectCheck
          :data-source.sync="dataSource"
          :scroll="{ x: true, y: 300 }"
          :remote-url="listRemoteUrl"
          :remoteData="listParams"
          :rowSelectType="2"
          :selectedRowKeys.sync="selectedRowKeys"
        >
          <p-table-column data-index="name" title="策略名称" :width="120" />
          <p-table-column data-index="conditionName" title="满足条件" :width="120" />
          <p-table-column data-index="transferStationOrder" title="中转顺序" :width="120" />
          <p-table-column data-index="memo" title="备注" :width="120" />
          <p-table-column data-index="statusName" title="状态" :width="120" />
          <p-table-column data-index="createperson" title="创建人" :width="120" />
          <p-table-column data-index="createtime" title="创建时间" :width="120" />
          <p-table-column data-index="updateperson" title="修改人" :width="120" />
          <p-table-column data-index="updatetime" title="修改时间" />
          <p-table-column key="action" title="操作" :width="150" fixed="right">
            <template slot-scope="text, record">
              <a-space>
                <a @click.stop="toEdit('view', record)">查看</a>
                <a @click.stop="toEdit('edit', record)">修改</a>
                <p-switch checked-children="启用" un-checked-children="停用" @change="changeSwitch(record)" :checked="record.status === 10 ? true : false" />
              </a-space>
            </template>
          </p-table-column>
        </p-table>
      </p-card>
    </p-spin>

    <edit v-else :editId="editId" :type="editType" :options="options" @back="editType = ''"></edit>
  </div>
</template>

<script>
import { initDictionary } from '@/blocks/utils';

import { Enable, Disable, listRemoteUrl } from './js/api';
import edit from './modules/edit';

export default {
  components: { edit },
  data() {
    return {
      spinning: false,
      editType: '', // 空-列表页 view-查看 edit-修改

      form: {},

      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl,
      listParams: {
        contractNo: '',
        contractName: '',
        fromDate: '',
        endDate: '',
      },
      options: {
        GENERAL_STATUS: [],
        TRANSFER_TIME_TYPE: [],
      },
    };
  },
  mounted() {
    initDictionary([{ objectcode: 'GENERAL_STATUS' }, { objectcode: 'TRANSFER_TIME_TYPE' }], true).then((res) => {
      for (const key in res) {
        this.options[key] = res[key];
      }
    });
  },
  methods: {
    toEdit(type, editData = { id: '' }) {
      this.editType = type;
      this.editId = editData.id;
    },
    onSearch() {
      this.detailDataSource = [];
      this.detailSelectedRowKeys = [];
      this.listParams = { ...this.form };
    },
    onReset() {
      this.$refs.formTarget.resetFields();
      this.form = {
        name: '',
        approvalFlowType: '',
        status: '',
      };
      this.onSearch();
    },
    onStatus(status) {
      const ids = this.selectedRowKeys;
      if (!ids.length) {
        this.$message.error('请选择数据');
        return false;
      }
      this.WhetherToEnable(status, ids);
    },
    changeSwitch(record) {
      this.WhetherToEnable(record.status === 10 ? 0 : 10, [record.id]);
    },
    WhetherToEnable(status, ids) {
      let self = this;
      let text = status ? '是否启用?' : '是否停用?';
      this.$confirm({
        title: text,
        onOk() {
          return new Promise((resolve, reject) => {
            if (status) {
              Enable(ids)
                .then((res) => {
                  if (res.success) {
                    self.$message.success(res.message);
                    self.onSearch();
                    resolve();
                  } else {
                    self.$message.error(res.message);
                    reject();
                  }
                })
                .catch((res) => {
                  self.$message.error(res.message);
                  resolve();
                });
            } else {
              Disable(ids)
                .then((res) => {
                  if (res.success) {
                    self.$message.success(res.message);
                    self.onSearch();
                    resolve();
                  } else {
                    self.$message.error(res.message);
                    reject();
                  }
                })
                .catch((res) => {
                  self.$message.error(res.message);
                  resolve();
                });
            }
          });
        },
      });
    },
  },
};
</script>