<template>
  <div>
    <basic-container v-if="isShow">
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :search.sync="query"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            @click="addButton"
            >创建单据
          </el-button>
        </template>
        <template slot="menu" slot-scope="{ row, index }">
          <el-button
            size="small"
            icon="el-icon-edit"
            type="text"
            @click="rowEdit(row)"
            >编辑
          </el-button>
          <el-button
            size="small"
            icon="el-icon-delete"
            type="text"
            @click="rowDel(row, index)"
            :disabled="row.status > 0"
            >删 除
          </el-button>
        </template>
        <template slot="contractNo" slot-scope="{ row }">
          <span style="color: #1e9fff;cursor: pointer;" @click="rowEdit(row)">
            {{ row.contractNo }}
          </span>
        </template>
      </avue-crud>
    </basic-container>
    <detailsPage
      v-if="!isShow"
      :detailData="detailData"
      @goBack="goBack"
    ></detailsPage>
  </div>
</template>

<script>
import { getList, remove } from '@/api/salesLead/index';
import detailsPage from './detailsPage.vue';
import { getToken } from '@/util/auth';
export default {
  data() {
    return {
      isShow: true,
      form: {},
      query: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        menuWidth: 120,
        tip: false,
        searchShow: true,
        searchMenuSpan: 24,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        searchIcon: true,
        align: 'center',
        searchIndex: 3,
        column: [
          {
            label: '线索编号',
            prop: 'leadCode',
            search: true,
            overHidden: true
          },
          {
            label: '提报客户',
            prop: 'customerName',
            width: 90,
            search: true,
            overHidden: true
          },
          {
            label: '联系人姓名',
            prop: 'contactName',
            width: 90,
            overHidden: true
          },
          {
            label: '联系电话',
            prop: 'contactPhone',
            overHidden: true
          },
          {
            label: '提报题目',
            prop: 'title',
            search: true,
            overHidden: true
          },
          {
            label: '结束时间',
            prop: 'endTime',
            overHidden: true
          },
          {
            label: '优先级',
            prop: 'priority',
            overHidden: true
          },
          {
            label: '来源',
            prop: 'source',
            width: 80,
            overHidden: true
          },
          {
            label: '线索组名称',
            prop: 'groupName',
            search: true,
            width: 100,
            overHidden: true
          },
          // {
          //   label: '索赔金额',
          //   prop: 'claimAmount',
          //   overHidden: true
          // },
          {
            label: '线索状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '待处理',
                value: 0
              },
              {
                label: '跟进中',
                value: 1
              },
              {
                label: '已转化',
                value: 2
              },
              {
                label: '已关闭',
                value: 3
              }
            ],
            overHidden: true
          },
          {
            label: '关闭原因',
            prop: 'closeReason',
            overHidden: true
          },
          {
            label: '备注',
            prop: 'remark',
            overHidden: true
          },
          {
            label: '制单人',
            prop: 'createUserName',
            overHidden: true,
            width: 80
          },
          {
            label: '制单日期',
            prop: 'createTime',
            type: 'date',
            overHidden: true,
            width: 100,
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd HH:mm:ss'
          },
          {
            label: '修改人',
            prop: 'updateUserName',
            overHidden: true,
            width: 80
          },
          {
            label: '修改日期',
            prop: 'updateTime',
            type: 'date',
            overHidden: true,
            width: 100,
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd HH:mm:ss'
          }
        ]
      },
      data: []
    };
  },
  components: {
    detailsPage
  },
  created() {
  },
  methods: {
    addButton() {
      this.isShow = false;
    },
    /**
     * @param {{ id: any; }} row
     */
    rowEdit(row) {
      this.detailData = {
        id: row.id
      };
      this.isShow = false;
    },
    // 删除
    /**
     * @param {{ item: number; id: any; }} row
     * @param {any} index
     */
    rowDel(row, index) {
      if (row.item == 1) {
        return this.$message.error('存在明细不允许删除');
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({ ids: row.id }).then(res => {
          this.onLoad(this.page, this.query);
          this.$message.success('成功删除');
        });
      });
    },
    searchReset() {
      this.query = this.$options.data().query;
      this.onLoad(this.page);
    },
    // 搜索按钮点击
    /**
     * @param {any} params
     * @param {() => void} done
     */
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    /**
     * @param {any} list
     */
    selectionChange(list) {
      this.selectionList = list;
    },
    /**
     * @param {any} currentPage
     */
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    /**
     * @param {any} pageSize
     */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    /**
     * @param {{ currentPage: any; pageSize: any; }} page
     */
    onLoad(page, params = {}) {
      let obj = {};
      obj = {
        ...Object.assign(params, this.query)
      };
      this.loading = true;
      getList(page.currentPage, page.pageSize, obj)
        .then(res => {
          this.data = res.data.data.records;
          this.page.total = res.data.data.total;
          this.$nextTick(() => {
            this.$refs.crud.doLayout();
            // this.$refs.crud.dicInit();
          });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 详情的返回列表
    goBack() {
      // 初始化数据
      if (JSON.stringify(this.$route.query) != '{}') {
        this.$router.$avueRouter.closeTag();
        this.$router.push({
          path: '/boxManagement/buyContainer/index'
        });
      }
      this.detailData = {};
      this.isShow = true;
      this.onLoad(this.page, this.query);
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-col-md-8 {
  width: 24.33333%;
}
::v-deep .el-table .cell {
  padding-right: 0px !important;
}
::v-deep .avue-crud .el-table .el-button.el-button--small {
  padding: 0px !important;
  margin-right: 0px !important;
}
</style>
