<template>
  <el-container class="content-height-fit">
    <inner-sidebar>
      <project-section-tree @node-click="handleTreeClick" />
    </inner-sidebar>
    <el-container class="resize-fit-container">
      <div class="app-container">
        <div class="filter-container blue-top-border">
          <el-input
            v-model="listQuery.content"
            placeholder="请输入内容"
            class="filter-item"
            clearable
            @change="getList"
          />
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >查询</el-button>
          <!-- v-if="listQuery.projectId && $requirePermission('DEMO.SAM.C')" -->
          <el-button

            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-edit"
            @click="handleCreate"
          >新增</el-button>
        </div>
        <div class="content-container">
          <el-table
            v-loading="listLoading"
            row-key="id"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%"
            height="calc(100vh - 276px)"
          >
            <div slot="empty">
              <span v-if="listQuery.projectId">没有数据</span>
              <span v-else>请选择项目或标段</span>
            </div>
            <el-table-column
              label="序号"
              type="index"
              align="center"
              width="80"
            />
            <el-table-column
              label="操作"
              align="center"
              width="260"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="{ row }">
                <!-- <el-button v-if="$requirePermission('ZZ.JS.CP')" type="primary" size="mini" @click="handlePermission(row)">授权</el-button> -->
                <el-button
                  v-if="$requirePermission('DEMO.SAM.U')"
                  type="primary"
                  size="mini"
                  @click="handleUpdate(row)"
                >修改</el-button>
                <el-button
                  v-if="$requirePermission('DEMO.SAM.D')"
                  size="mini"
                  type="danger"
                  @click="handleDelete(row)"
                >删除</el-button>
              </template>
            </el-table-column>
            <el-table-column
              label="项目名称"
              prop="projectName"
              align="center"
            />
            <el-table-column
              label="标段名称"
              prop="sectionName"
              align="center"
            />
            <el-table-column label="内容" prop="content" align="center" />
          </el-table>
          <pagination
            :total="total"
            :skip-count.sync="listQuery.skipCount"
            :max-result-count.sync="listQuery.maxResultCount"
            @pagination="getList"
          />
        </div>
        <el-dialog
          :title="textMap[dialogStatus]"
          :close-on-click-modal="false"
          :visible.sync="dialogFormVisible"
        >
          <el-form
            ref="dataForm"
            v-loading="formLoading"
            :rules="rules"
            :model="sample"
            label-position="top"
            label-width="120px"
          >
            <el-row :gutter="16">
              <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
                <el-form-item label="标段" prop="sectionId">
                  <el-select v-model="sample.sectionId" class="w100p">
                    <el-option
                      v-for="item in sections"
                      :key="item.id"
                      :value="item.id"
                      :label="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
                <el-form-item label="内容" prop="content">
                  <el-input v-model="sample.content" type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button
              :disabled="formLoading"
              @click="dialogFormVisible = false"
            >取消</el-button>
            <el-button
              :disabled="formLoading"
              type="primary"
              @click="dialogStatus === 'create' ? createData() : updateData()"
            >确认</el-button>
          </div>
        </el-dialog>
      </div>
    </el-container>
  </el-container>
</template>

<script>
import * as sampleApi from '@/api/sample'
import { lookupConstructions } from '@/api/backbone/section'
import ProjectSectionTree from '@/views/projects/components/ProjectSectionTree'

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import InnerSidebar from '@/components/InnerSidebar'

export default {
  name: 'SampleMng',
  directives: { waves },
  components: { Pagination, ProjectSectionTree, InnerSidebar },
  data() {
    return {
      processDialogFormVisible: false,
      showAuditProgressDialog: false,
      list: [],
      sample: {
        id: undefined,
        projectId: undefined,
        sectionId: undefined,
        content: undefined,
        attachments: []
      },
      total: 0,
      listLoading: false,
      listQuery: {
        projectId: undefined,
        sectionId: undefined,
        skipCount: 0,
        maxResultCount: 20
      },
      dialogFormVisible: false,
      dialogStatus: '',
      formLoading: false,
      textMap: {
        update: '修改',
        create: '创建'
      },
      sections: [],
      rules: {
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { max: 64, message: '长度必须少于512个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'sample.projectId': function(val) {
      lookupConstructions(val).then((res) => {
        this.sections = res
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleTreeClick(data, node) {
      if (data.isProject) {
        this.listQuery.projectId = data.id
        this.listQuery.sectionId = undefined
      }
      if (data.isSection) {
        this.listQuery.projectId = data.projectId
        this.listQuery.sectionId = data.id
      }
      console.log(data.id)
      debugger
      this.getList()
    },
    getList() {
      this.listLoading = true
      sampleApi
        .getList(this.listQuery)
        .then((res) => {
          res.items.forEach((t) => {
            t.attachments = []
          })
          this.list = res.items
          this.total = res.totalCount
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    resetSample() {
      this.sample = {
        id: undefined,
        content: undefined,
        projectId: this.listQuery.projectId,
        sectionId: this.listQuery.sectionId,
        attachments: []
      }
    },
    handleCreate() {
      this.resetSample()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.formLoading = true
          sampleApi
            .create(this.sample)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    handleUpdate(row) {
      this.sample = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const sampleData = Object.assign({}, this.sample)
          this.formLoading = true
          sampleApi
            .update(sampleData.id, sampleData)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        sampleApi
          .remove(row.id)
          .then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .finally(() => {
            this.listLoading = false
          })
      })
    }
  }
}
</script>
