<template>
  <div class="ele-body">
    <el-card shadow="never">
      <search
        ref="search"
        @search="search"
        :options_groupId="dict.groupId"
        :options_factory="dict.factory"
      ></search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :page-size="pageSize"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
        @filter-change="selectType"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
            v-if="$hasPermission('main:factoryarea:saveOrUpdate')"
          >
            添加
          </el-button>
        </template>

        <template v-slot:enabled="{ row }">
          {{ dict.enabled[row.enabled] }}
        </template>

        <template v-slot:workCenterList="{ row }">
          <p v-for="item in row.workCenterList">
            {{ item.name }}
          </p>

          <!-- {{ row.workCenterList }} -->
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('copy', row)"
            v-if="$hasPermission('main:factoryarea:saveOrUpdate')"
          >
            复制
          </el-link>

          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
            v-if="$hasPermission('main:factoryarea:saveOrUpdate')"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此产线吗？"
            @confirm="remove(row)"
            v-if="$hasPermission('main:factoryarea:delete')"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <edit
      ref="edit"
      @done="done"
      :options_groupId="dict.groupId"
      :options_factory="dict.factory"
    ></edit>
  </div>
</template>
<script>
  import tabMixins from '@/mixins/tableColumnsMixin';

  import search from './components/search.vue';
  import edit from './components/edit.vue';
  import { getFactoryarea, deletefactoryarea } from '@/api/factoryModel';
  import { listOrganizations } from '@/api/system/organization';
  export default {
    mixins: [tabMixins],
    components: {
      search,
      edit
    },
    data() {
      return {
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center'
          },
          {
            prop: 'code',
            label: '产线编码'
          },
          {
            label: '产线名称',
            prop: 'name'
          },

          {
            label: '所属工厂',
            prop: 'factoryName'
          },

          {
            label: '所属厂房',
            prop: 'workshopPlanName'
          },

          {
            label: '所属车间',
            prop: 'workshopName'
          },

          {
            label: '工作中心',
            prop: 'workCenterList',
            slot: 'workCenterList'
          },

          {
            label: '所属区域',
            prop: 'areaName'
          },

          {
            label: '省/市/区',
            prop: 'extInfo.location'
          },
          {
            label: '详细地址',
            prop: 'extInfo.locationDetail'
          },
          {
            //修改此prop名称时，请同步修改columnKey属性和下方selectType方法
            label: '状态',
            prop: 'enabled',
            slot: 'enabled',
            filters: [
              { value: 1, text: '生效' },
              { value: 0, text: '未生效' }
            ],
            filterMultiple: false,
            columnKey: 'enabled'
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        dict: {
          groupId: [],
          factory: [],
          enabled: {
            1: '生效',
            0: '未生效'
          }
        },
        pageSize: this.$store.state.tablePageSize,
        cacheKeyUrl: '015ef54f-factoryModel-productionLine'
      };
    },
    created() {
      this.getGs();
      this.getFactoryarea();
    },
    methods: {
      selectType(value) {
        let where = {};
        if (value.enabled.length > 0) {
          where['enable'] = value.enabled[0];
        }
        this.search(where);
      },
      datasource({ page, where, limit }) {
        return getFactoryarea({
          ...where,
          pageNum: page,
          size: limit,
          type: 4
        });
      },
      search(where) {
        this.$refs.table.reload({
          where: where,
          page: 1
        });
      },
      openEdit(type, row) {
        if (row) {
          if (typeof row.mainFactoryCapacityList == 'string') {
            this.$set(row, 'mainFactoryCapacityList', []);
          }
        }
        this.$refs.edit.open(type, row);
      },
      // 获取公司数据
      getGs() {
        listOrganizations().then((list) => {
          console.log(list);
          this.dict.groupId = list;
        });
      },

      // 获取工厂数据
      getFactoryarea() {
        let par = {
          type: 2,
          size: 9999
        };
        getFactoryarea(par).then((res) => {
          this.dict.factory = res.list;
        });
      },
      remove(row) {
        deletefactoryarea(row.id)
          .then((message) => {
            this.$message.success(message);
            this.done();
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      done() {
        this.$refs.search.search();
      }
    }
  };
</script>
